[Qt-interest] squeeze a string for display

Eirik Ulvik eiriku at simsurgery.com
Thu Jun 3 22:54:40 CEST 2010



Den 03.06.2010 22:08, skrev Daniel Franke:
> 
> Hi all.
> 
> I want to compress/squeeze a string for display, but can't find the proper way 
> to do it.
> 
> Consider:
> 
>       +---------------------------------+
>       | A string too long for this widget as it overruns the border
>       +---------------------------------+
> 
> I'd like to display this either as
> 
>       +---------------------------------+
>       | A string too long for this ...  |
>       +---------------------------------+
> or
>       +---------------------------------+
>       | A string too ... the border     |
>       +---------------------------------+
> 
> instead of increasing the widget, which is what I currently have:
> 
>       +------------------------------------------------------------+
>       | A string too long for this widget as it overruns the border|
>       +------------------------------------------------------------+
> 
> Could someone be so kind to point me to the appropriate section of the docs?
> I can't find anything myself.
> 
> Thanks
> 
> 	Daniel
> 
> 
> P.S. In case it's significant: the string to squeeze is the title of a 
> groupbox which shall be a filename - depending on the filesystem, this title 
> may be rather long.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest


What you are looking for is elided text. There is some support for it in
QFontMetrics. I had the same proiblem as you and solved it by
subclassing QLabel and overriding the paint event. For me the paint
event looks like this:

void ElideLabel::paintEvent(QPaintEvent *event)
{
  QPainter p(this);
  QFontMetrics fm(font());
  if (fm.width(text()) > contentsRect().width()) {
   QString elided_txt;
   if(this->elided_pos == ELIDE_MIDDLE)
     elided_txt = this->fontMetrics().elidedText(text(),
                                                 Qt::ElideMiddle,
                                                 rect().width(),
                                                 Qt::TextShowMnemonic);
   else
     elided_txt = this->fontMetrics().elidedText(text(),
                                                 Qt::ElideRight,
                                                 rect().width(),
                                                 Qt::TextShowMnemonic);

   p.drawText(rect(), elided_txt);
  }
  else
    QLabel::paintEvent(event);
}

this->elided_pos is just a member variable in order for you to be able
to configure where the elide (...) should appear.

Good luck.

Eirik

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100603/56f03368/attachment.bin 


More information about the Qt-interest-old mailing list