[Qt-interest] setElideMode feature on QLabel?

Eirik Ulvik eiriku at simsurgery.com
Tue Jan 26 16:11:34 CET 2010



Den 26.01.2010 16:03, skrev Santhosh Y:
> Hi,
>
> Can I set    Elide Mode for eg: Qt::ElideRight , to the QLabel text.
>
> I am using QLabel object for showing the name of a file.
> This name can be a huge string and in such a case I want to show
> ellipsis to its right.
>
> Can I do this?
> Is it not possible with QLabel such a feature?
> Please suggest me how to solve about this issue.
>
> Thanks in advance.
>
>    
You should subclass QLabel and override the paintEvent. My painEvent 
function is:

     void ElideLabel::paintEvent(QPaintEvent *event)
     {
       QPainter p(this);
       QFontMetrics fm(font());
       if (fm.width(text()) > contentsRect().width()) {
         QString elided_txt;
         if(ELIDE_MIDDLE) // ELIDE_MIDDLE is part of a class enum
           elided_txt = this->fontMetrics().elidedText(text(), 
Qt::ElideMiddle, rect().width(), Qt::TextShowMnemonic); //This is the 
key line.
         else { //Handle all other elide modes you want to support.
          }
         p.drawText(rect(), elided_txt);
       }
       else
         QLabel::paintEvent(event);
     }

Eirik



More information about the Qt-interest-old mailing list