0

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

ymoreau
  • 3,402
  • 1
  • 22
  • 60
Rami Rosenbaum
  • 467
  • 5
  • 18
  • Do you only want to display some text? Then a `QLabel` would be the better solution. I wrote a class to display a scrolling text: http://stackoverflow.com/questions/10651514/text-scrolling-in-qlabel/10655396#10655396 – leemes Jul 03 '12 at 19:56

1 Answers1

2

You can get the width of a text using QFontMetrics:

int textWidth = myTextEdit->fontMetrics().width(myTextEdit->text());
leemes
  • 44,967
  • 21
  • 135
  • 183
  • Problem is, if you have multiple lines, all of them get factored into the width instead of just the longest line – mrg95 Jul 29 '17 at 01:03