[Qt-interest] Calculating text size in QLineEdit

Andre Somers andre at familiesomers.nl
Tue Mar 8 10:20:23 CET 2011


Op Di, 8 maart, 2011 10:06 am, schreef Matthias Paul:
> Hello!
>
> I created a QLineEdit subclass, which automatically displays a truncated
> text if the original text is too long for the widget's size, so that
> there's no need to scroll for the user. Instead a tooltip with full text
> is displayed.
>
> QFontMetrics calculates the size required for the displayed text based on
> QWidget's size() (provided by ResizeEvent). Still, I cannot figure out how
> to get the the text label size _minus_ widget's borders and margins. Guess
> I have to use QStyle somehow, so can someone give me a hint please?

Perhaps you can use QStyle::subElementRect, using
QStyle::SE_LineEditContents for the element argument, and a style option
initialized with your line edit widget as the style option? That should
give you the rect that is available to draw the text.

André





More information about the Qt-interest-old mailing list