简体   繁体   中英

How to align QPushButton and QComboBox in a horizontal layout?

I am trying to align QPushButton and QComboBox in a horizontal layout, but for some reason I cannot do it and I always get this kind of alignment:

在此处输入图像描述

So, QComboBox is always a bit higher than it should be.

This is on macOS Big Sur 11.2.3 and version of Qt is 5.12.10. Is there some kind of workaround or fix for this?

Try to set the layout stretch of the items to the same value. 在设计器中

or with code:
horizontalLayout->setStretch(1 /* Item index */, 1 /* Stretch Factor */) // Do that for all items. https://doc.qt.io/qt-5/qboxlayout.html#setStretch

I think I found a workaround. I found this topic on Qt Forum and it seems that for some reason QPushButton is drawn that way. So for now I have overridden method paintEvent() which now draws button on more or less good position:

virtual void paintEvent(QPaintEvent* event) override {
    QStylePainter p(this);
    QStyleOptionButton option;
    initStyleOption(&option);
    option.rect = rect().adjusted(0, -2, 0, -2);
    p.drawControl(QStyle::CE_PushButton, option);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM