Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Autoadjust text into QLabel
gerteksa last edited by
I'm developing an application that it will be run into an embedded system to show images and text in a 48x80 LED panel. For images I'm using QLabel, that "autoadjust" the image size to 48x48 pixels with the method setScaledContents(true):
To show the text, I'm using QLabel too. This is the code:
However, the text is not autoadjust into the QLabel size, and if my text is a bit longer, is cut, for example if I put: THIS IS AN EXAMPLE in the code, it appears THIS IS A, and lost part of the text. Why it happens? With the image setScaledContents method the image works well, but why not with the text? Which solution do you propose?
Hi and welcome to devnet,
As stated in the documentation, the scaling works for QPixmaps. What you could do is write on your pixmap and then set it on your label or set a smaller font
Hope it helps
andre last edited by
Actually, it doesn't really say that. Here is what it says:
[quote]This property holds whether the label will scale its contents to fill all available space.
When enabled and the label shows a pixmap, it will scale the pixmap to fill the available space.
This property's default is false.[/quote]
It explains what happens if the label shows a pixmap, but it doesn't say that it doesn't work with text. At least, that's how I read it. I know it doesn't, but the documentation is not very clear on that, and the API method name suggests that it should work on text as well. Otherwise, the method name should have been setScaledImage or something like that.
To me, this smells like a feature that was never quite completed and was conceived to work also for text, but only got implemented for images.
As to a solution: I can only think of using QFontMetrics (single line) or QTextDocument (multiple lines) to get size information on your text, and using that information to adjust the font size. You may have to iterate to get to a good result.