I need a QLabelwidth that should not adapt to the contained text, but can be changed by the user (or, more precisely, the layout). If the text is too long for the width QLabel, you should just crop it.
This question is somehow the opposite
How to make QLabel the width of the width for placing text . However, the content of this question did not help me. Also, the installation of text in QLabel in the layout does not change.
Background
The QLabelidentifiers (single words) coming from another system will be displayed in. Sometimes these identifiers change many times per second, which makes the entire layout flicker. QLabelis part of the vertical dock, so the width of the dock flickers.
On the other hand, the user must decide how many identifiers he could see. Therefore, I want to allow the user to change the width of the dock so that the width QLabeladapts to this.
Resolving Solutions
To do this, I set the horizontal size policy to QSizePolicy::Preferredand got my own shortcut class from QLabel, in which I redefined sizeHint()to return a fixed size. But that did not change the behavior.
I know that I could apply QFontMetricsto calculate the width of the text, and then cut it off to fit the width QLabel. But this does not seem to be the right solution, especially since I would like the last letter itself to be truncated if it is completely inappropriate to let the user know that the identifier is too long to display.
- Qt 5.5.1
- GCC 5.4.0
- Ubuntu 16.04