[Qt-interest] squeeze a string for display

Jason H scorp1us at yahoo.com
Thu Jun 3 22:35:44 CEST 2010


What you want are ellipsis (...)
And I don't know of any Qt function that will do it for you. The basic steps are:
1 Obtain the QFontMetrics for the font being used. 
2 Compare the width of the string to be rendered with the available width.
3 Replace a character with "."
4 Go to 2 until it is less than the available width.

For 3, you have a couple ways to choose how to pick the letter to replace.
a. From the front
b. From the back
c. In the middle
d. (My favorite, if working with titles) I find this way produces somewhat intelligent abbreviations automatically
   Replace "the " with "t"
   Replace " and " with "&"
   Not replace capital letters, keeping as many latter letters as possible. (Nine Inch Nails = NINails)

HTHelps (my method D :-))




----- Original Message ----
From: Daniel Franke <franke.daniel at gmail.com>
To: qt-interest at trolltech.com
Sent: Thu, June 3, 2010 4:08:20 PM
Subject: [Qt-interest] squeeze a string for display


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



      




More information about the Qt-interest-old mailing list