简体   繁体   English

如何在水平布局中对齐 QPushButton 和 QComboBox?

[英]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:我试图在水平布局中对齐QPushButtonQComboBox ,但由于某种原因我无法做到,而且我总是得到这种 alignment:

在此处输入图像描述

So, QComboBox is always a bit higher than it should be.所以, QComboBox总是比它应该的高一点。

This is on macOS Big Sur 11.2.3 and version of Qt is 5.12.10.这是在 macOS Big Sur 11.2.3 上,Qt 的版本是 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 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.我在 Qt 论坛上找到了这个主题,似乎出于某种原因QPushButton是这样绘制的。 So for now I have overridden method paintEvent() which now draws button on more or less good position:所以现在我已经覆盖了方法paintEvent() ,它现在在或多或少好的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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