Important: Please read the Qt Code of Conduct -

Setting a ComboBox's width to fixed in a QHBoxLayout

  • I use setFixed function on a comboBox, then add it to a QHBoxLayout...the width of the combobox adjusts to fill the QHBoxLayout's extra space, but the height stays fixed as I want it to.

    I have tried using setMaximumWidth...layout->setStretchFactor...size policy to fixed...why is my comboBox stretching?

    How can I keep the combobox's width from adjusting? Thanks

  • Lifetime Qt Champion

    You can use
    setMaximumWidth to set a max.

  • I have tried it, it does not work

  • Lifetime Qt Champion

    Hmm. Strange. It works here.

    It stays 64 pixels.

  • Yeah, this code seems to resize it someway somehow after the fact. I set it to size I want, then check it's size with ->size(), then add to layout, check size again, and it is still the size I set.

    I think this means it changes the size somewhere else in program after, but I do not know where.

    What I am looking for is a method to keep my comboBox's width throughout the rest of the program...why doesn't setFixedSize work...?

  • Lifetime Qt Champion

    The layout controls the size. Its the same with setGeometry. It wont work when in layout.
    setFixedSize should also work. Its the same as setting equal values for both min/max.

  • Lifetime Qt Champion


    IIRC, the size policy might help.

  • @SGaist @mrjj SizePolicy and SetFixedSize did not work. I was able to get the original QComboBox less wide by setting

    comboBox->setStyleSheet("QComboBox{margin-left: 80px;} QComboBox QAbstractItemView{margin-left:80 px;}");

    This makes this happen...I am beyond frustrated by this. What in the WORLD is causing this and why does Qt have so many areas where things just DON'T WORK.

    The highlighting even works in the new version! The only thing I need to remove now is the extra blankspace that is the same length as the original box itself...

    alt text

  •     comboBox->setMaximumWidth(20);
        comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        comboBox->setFixedSize(20, 20);
        (add comboBox to HBoxLayout here)
        qDebug() << comboBox->size();

    This debug prints out 20,20 as the size...but my comboBox is definitely not 20, 20!!!!! How can this be happening!?!?!

Log in to reply