[Interest] QComboBox is positioned incorrectly in a layout on macOS
Indi Kernick
kerndog73 at gmail.com
Tue Jul 23 03:07:03 CEST 2019
I posted about this on the forums
<https://forum.qt.io/topic/105191/why-isn-t-a-qcombobox-positioned-correctly-in-a-layout/10>
and
it was recommended that I post a message here.
A QComboBox is shifted by a few pixels when it's put in a layout. This
causes the QComboBox to overlap neighbouring widgets. The issue can be
reproduced using this example:
#include <QtGui/qpainter.h>#include <QtWidgets/qcombobox.h>#include
<QtWidgets/qboxlayout.h>#include <QtWidgets/qmainwindow.h>#include
<QtWidgets/qapplication.h>
template <typename Base>
class Rectangle : public Base {
public:
explicit Rectangle(QColor color)
: color{color} {
Base::setFixedSize(100, 20);
}
private:
QColor color;
void paintEvent(QPaintEvent *) override {
QPainter{this}.fillRect(Base::rect(), color);
}
};
int main(int argc, char **argv) {
QApplication app{argc, argv};
QMainWindow window;
QWidget central;
window.setCentralWidget(¢ral);
QHBoxLayout layout{¢ral};
layout.setSpacing(0);
layout.setContentsMargins(0, 0, 0, 0);
layout.addWidget(new Rectangle<QWidget>{Qt::red});
layout.addWidget(new Rectangle<QComboBox>{Qt::blue});
window.show();
return app.exec();
}
You can easily switch the between QWidget and QComboBox using the
template. Here's a screenshot of what I'm seeing with the combo box:
[image: combobox.png]
The blue rectangle (QComboBox) is overlapping the red one (QWidget). I
expect to see this:
[image: widget.png]
The two widgets are sitting next to each other in the layout.
I'm hoping to understand why this occurs. This seems like a bug to me. A
workaround that I'm yet to try is to offset the painting of the combo box
to compensate. That's not ideal though. I tried adding a margin using
stylesheets but that didn't do anything. Perhaps there's a better
workaround or even a fix?
I'm able to reproduce the issue on *Qt 5.13* and *macOS 10.14.6*.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190723/7bf73d15/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: combobox.png
Type: image/png
Size: 35720 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190723/7bf73d15/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: widget.png
Type: image/png
Size: 35793 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190723/7bf73d15/attachment-0001.png>
More information about the Interest
mailing list