PDA

View Full Version : Vertical alignment for QComboBox



Julieng031
17th April 2014, 11:46
Hello,

I am trying to position a QComboBox in a layout but I cannot succeed to do it, could you help me ?

Indeed, I have a QVBoxLayout in which I have added : a QLabel (visible), a QComboBox (visible) and a QWidget (not visible at the beginning). The result is that the QLabel and the QCombobox are positioned at the bottom of the QVBoxLayout. When I turn the QWidget to visible, both QLabel and QComboBox goes at the top (pushed by the QWidget).

Is there a way to have, from the beginning, the QLabel and the QComboBox positionned at the top. I have found "setAlignment(Qt::AlignTop)" for the QLabel but nothing for the QComboBox.

Thanks in advance.

Added after 5 minutes:

I found that : "setDirection(QBoxLayout::TopToBottom)", applied to the QVBoxLayout.

However, QLabel is now positionned in the middle and the QComboBox at the bottom of the QLayout.

adutzu89
17th April 2014, 12:12
Try setting QSizePolicy (http://qt-project.org/doc/qt-5/qsizepolicy.html#setVerticalPolicy) of QLabel.
I see that QSizePolicy also has void QSizePolicy::setVerticalPolicy(Policy policy) function.

So something like label->setVerticalPolicy(QSizePolicy::Fixed) might solve it.

anda_skoa
17th April 2014, 12:23
One other thing you can try is adding a QSpacerItem or a stretch, so it takes the available space if the third widget is hidden.

Cheers,
_

Julieng031
17th April 2014, 13:10
I tried to add "Qt::AlignTop" for the third parameter of "addWidget()" but it did not worked.

Finally, I solved it by adding "QVBoxLayout->setAlignment(Qt::AlignTop)".

Thanks for your answers !