Important: Please read the Qt Code of Conduct -

How can I prevent a wordWrapping QLabel from clipping on parent resize?

  • Hi :-)

    I have a dialog displaying a QLabel with wordWrap set to true and a text spanning multiple lines. I also do setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum). The result on loading is:


    I can however make the dialog vertically smaller by some amount, and then, the text is clipped:


    How can I prevent this clipping? Thanks for all help!

  • Lifetime Qt Champion

    In what way prevent it ?
    It needs more space for full text so what should it do ?

    You can set a minimum size based on text size so it cant get too small.
    Or you can create a custom QLabel that can alter the font size so full text still fits to the
    new rect.

  • @mrjj
    I don't know, but I can only guess OP is claiming he does not have a maximum height on the multi-line label, yet he still sees clipping at the bottom. Which would make one think: this has nothing to do with wrapping, and he does have a max height somewhere he needs to track down?

  • Lifetime Qt Champion

    That could also be.
    Often poster wants the text to shrink :)

  • I don't understand why the label can be displayed so (vertically) small that the content is clipped, altough I set QSizePolicy::Minimum as the vertical size policy. I would like to ensure that the label can't be made smaller as it's content, so that no clipping can occur. And I thought Qt would take care of that it I set the correct size policy …

    I don't want the text size to change. Simply, the parent QDialog should reject to be dragged vertically smaller if the space to display all text is not sufficient anymore.

    According to teh docs, QSizePolicy::Minimum effects that the widget can't be smaller than it's sizeHint. So doesn't the label calculate it correctly? Shouldn't it be the size that it needed to display all text without clipping?

  • @l3u_
    I believe your broad understating of the sizing is correct. Therefore I'm thinking that something is limiting the vertical size of your label. (Assuming Qt is not mis-measuring the height of your font/lines.) I don't think that the fact you are word wrapping should be the issue (e.g. comment out the wordWrap = true, does that make any difference?).

    If it were me, apart from looking for any maximum/fixed heights anywhere, I'd copy just what you need for this from your code into a little standalone project, and play with it there....

  • I played around with different size policies in my dialog. Maybe, this is caused somewhere else, the dialog I use is derived from a basic "dialog with a title label" class, which creates layouts, the derived class also adds layouts … seems to be a bit complicated ;-)

    Well, let's see …

  • This thread is quite old, nevertheless, I stumbled upon it again with the very same problem.

    I tried to reproduce this behavior with a minimal example to track it down. After all, I think I found the solution. It's not enough to set QSizePolicy::Minimum to the QLabels with the wordwrapped text. Widgets containing them (in the current case a QTabWidget) also have to be set to use QSizePolicy::Minimum vertically.

    After having done that, I couldn't drag the dialog smaller than it should be and no clipping occured anymore. So the solution is to set the right QSizePolicy to the right widgets, which may cause some headache ;-)

Log in to reply