QTextEdit display width vs text width

1.1k Views Asked by At

I'm creating a 'scrolling-text' class in Qt, using a QTextEdit (read-only, no scrollbars, moveCursor) and a QTimer - simple and working.

My problem is when the text sent to the class is shorter (narrower) than the QTextEdit-box. Silly, I agree, but, being new to Qt, I didn't find an easy way to compare the width of the given text (depending on the font) and the actual width that can be displayed inside the QTextEdit (after calculating the FrameStyle, etc.). I presume I need to calculate the pixels.

Any ideas? Thanks

1

There are 1 best solutions below

1
On

You can get the width of a text using QFontMetrics:

int textWidth = myTextEdit->fontMetrics().width(myTextEdit->text());