[Qt-interest] Measuring the display width of text
william.crocker at analog.com
william.crocker at analog.com
Sun Feb 28 04:53:28 CET 2010
Nikos Chantziaras wrote:
> On 02/27/2010 02:55 AM, william.crocker at analog.com wrote:
>>> 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.
>> Binary search for the width and
>> cache what you learn.
>
> Thanks. I'm using binary search now to determine the length, but can
> you explain a bit more about what you mean with caching?
If you are dealing with a finite number of strings
(or a recurring set) you could cache the kept width
for string/width pairs using a QMap ( QPair<QString,int> to int ).
Or, if the font is constant width, the number of chars that
will fit a given width would always be the same.
Or you could start the search from the width that fit
last time.
Or you could start the search based on the fit
for an average character width.
> _______________________________________________
> 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