[Qt-interest] squeeze a string for display

Daniel Franke franke.daniel at gmail.com
Thu Jun 3 22:35:18 CEST 2010


On Thursday 03 June 2010 22:20:23 Colin S. Miller wrote:
> > Could someone be so kind to point me to the appropriate section of the
> > docs? I can't find anything myself.
> 
> You could either
> 1) take a peek at the code.
> 2) layout the qgroupbox with no label, then lock its size, and finally set
> the label.

Hi Colin,

thanks for the hints.

After writing the initial mail it took me another 5 minutes to finally find 
QFontMetrics::elidedText() which does exactly what I want ^^

For my case:

  QGroupBox *group;
  QString filename;
  // ...
  QFontMetrics metrics(QFontMetrics(group->font());
  group->setTitle(metrics.elidedText(filename, Qt::ElideMiddle,
                                     group->width() * 0.75));

This gives a nice label 75% of the size of the widgets within the group box 
with the "middle part" of the filename omitted.

Cheers

	Daniel



More information about the Qt-interest-old mailing list