Important: Please read the Qt Code of Conduct -

QLabel with wordwrap only fills half of available horizontal space

  • I've run into this problem on a couple of occasions and I'm starting to think it's a QT bug but want to post before coming to that conclusion. My issue is that when I enable word wrap on a label, it only goes half way across the available horizontal space before going to the next line. I think that QStackWidget may have something to do with it as it has been involved in every instance that I've seen this occur.

    My setup is very simple, it's a QStackWidget that holds a number of widgets, but for this example it currently only holds one QVBoxLayout with a QLabel and a button. The annoying thing is I know it knows there is more space because if I center the label it goes right in the middle where you would expect it too, but still is only half the size it should be. My current work around is to setMinimumWidth with double it's current size. I've attached a picture to help illustrate the issue.

    I've tried every combination of SizePolicy you can imagine, but it doesn't make a difference. I've also tried other layouts and it seems to effect all of them. Oddly, when I use QHBoxLayout the label will take up the first half of the the first row and the button will take up the 2nd half, but the text still only fills half the label.

    ! showing bad label word wrap)!

  • I don't know if that fixes your problem, but you can try to change the QLabel's sizePolicy.
    When using QLabels in a QLayout, the QLabels inherit the QLayout's sizePolicy, so maybe you have to set sizePolicy to QSizePolicy::Maximum or something like that... Try it and tell me if it works :-)

  • Thank you for the reply Binary91, but I've tried every combination of SizePolicy you can imagine with no luck :/

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show the code you use to setup your widgets ?

Log in to reply