[Qt-interest] Measuring the display width of text

Nikos Chantziaras realnc at arcor.de
Sat Feb 27 01:11:43 CET 2010


On Windows, one can use GetTextExtentExPoint():

   http://msdn.microsoft.com/en-us/library/aa911439.aspx

to determine how many characters of a given string in a specific font 
fit within the specified space.  I have an application that needs to do 
that, and it needs to do that a lot.  GetTextExtentExPoint() is very 
fast, but I can't find an equivalent in Qt.

Instead, it seems I am forced to call QFontMetrics::size() repeatedly 
while removing trailing characters from the string until enough 
characters have been remove for the string to fit a given width.  As you 
can imagine, performance suffers.

Is there no better way?  QFontMetrics::elidedText() comes close to be 
useful for this, but not quite.



More information about the Qt-interest-old mailing list