[Qt-interest] Finding the displayed width of rich text

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Sun Dec 20 07:26:15 CET 2009


Hi John, 

I suggest using QTextDocument with the same text.  Then use size or
idealSize. 

Tony


> -----Original Message-----
> From: qt-interest-bounces at trolltech.com 
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of John Posner
> Sent: Sunday, 20 December 2009 07:48
> To: qt-interest at trolltech.com
> Subject: [Qt-interest] Finding the displayed width of rich text
> 
> 
> I've been using plain text to display a fraction as the text 
> of a QLabel:
> 
>   "2/5"
> 
> But now, I'd like to use rich text to make a better-looking fraction, 
> using the superscript-and- subscript technique:
> 
>   "<sup>2</sup>/<sub>5</sub>"
> 
> I need to know the size of the displayed fraction, in order to select 
> the optimal point-size for the label. QFontMetrics.width() 
> does the job 
> for the plain-text label, but for the rich-text label, it returns the 
> size of the 25-character string, not the size of the 3-character 
> superscript-and-subscript string.
> 
> I found a workaround: find the size of the QLabel widget 
> itself, which 
> is just big enough to accommodate the rich-text label. But 
> I'm looking 
> for other solutions (because I want to fiddle with the label's size). 
> Can anyone provide advice?
> 
> Tx,
> John




More information about the Qt-interest-old mailing list