Important: Please read the Qt Code of Conduct -

How to stop QLabel and QPushbutton from expanding????

  • This is driving me crazy. I just want a QLabel and a QPushButton, each initialized with plain text, to sit together on the left side of one row inside an enclosing widget with a QVBoxLayout; together they are much narrower than the enclosing widget. No matter what I have tried they expand in one way or another. As the QPushButton text is either "+" or "-", I could call setMaximumSize(16) and that works for it; my class doesn't know the width of the label text. But there must be a better way. No attempt using QSizePolicy has worked for me.

    How can I get them to look like this at the left side of the enclosing widget:
    Label Text [+]
    (here the QPushButton is mimicked by [].)

  • So you have a button of known size, and a label of unknown size that are contained in a widget and you want their placement to be fixed.

    Do you have to place them within the QVBoxLayout? Why not tie them directly to the parent and use setGeometry to fix their placement within the parent?

  • If I attempted to use fixed geometry in the parent, I would have to carefully keep track of the pixel position of every widget in it; there are a lot of them. That's a LOT of work, and would be quite fragile; the whole point of using layouts is to avoid that sort of thing. Moreover, I don't know how much space to allocate for each.

    Why is it so hard in Qt to place widgets side-by-side without expanding? Or am I missing something rather obvious? After all, in HTML that's just natural. As it happens, I am translating dialog boxes from HTML to Qt (long, sad, story).

    I guess I'm looking for the analog to Java's FlowLayout....

Log in to reply