Is there any way to get QPushButtons to render as fully opaque in macOS dark mode?
-
I'm updating an old app to properly support dark mode in macOS, and in one case a
QPushButtongets positioned over top of a background with gridlines. In light mode this is fine, but in dark mode the button is rendered as partially transparent like so:
This is no good! I need the button to be opaque, but still render as native style macOS button. So I can't use
QPaletteas none of its roles save for text color have any effect on macOS buttons, and setting its background using a style sheet forces it to draw as non-native. Is there any way to do this? -
Hi,
Which version of Qt are you using ?
Which version of macOS are you running ?
Can you provide a minimal compilable example that shows this behaviour ? -
Hi,
Which version of Qt are you using ?
Which version of macOS are you running ?
Can you provide a minimal compilable example that shows this behaviour ?@SGaist I'm using Qt 6.2.3, but it looks like the issue affects pretty much all versions. I'm working in all supported versions of macOS,i.e. 10.14, 10.15, 11, 12 and 13.
Here is some code demonstrating the issue:
// main.cpp: #include <QApplication> #include <QWidget> #include <QPushButton> #include "MyWidget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWidget w; QPushButton *button = new QPushButton("Text", &w); button->move(10, 10); w.show(); a.exec(); return 0; } // MyWidget.h: #include <QWidget> #include <QPainter> class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) {}; void paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::red); painter.drawLine(5, 0, 5, 100); painter.drawLine(15, 0, 15, 100); painter.drawLine(25, 0, 25, 100); painter.drawLine(35, 0, 35, 100); painter.drawLine(45, 0, 45, 100); painter.drawLine(55, 0, 55, 100); } };How it looks in light mode in 10.14:

How it looks in dark mode in 10.14:

And the same issue is present in macOS 12:

-
@gvanvoor said in Is there any way to get QPushButtons to render as fully opaque in macOS dark mode?:
Have yout tried setting the Qt::WA_OpaquePaintEvent attribute on the button?
That's something of an improvement:

Now there's a dark gray background around the button that doesn't match the actual background, so it still doesn't look right. Unfortunately that background color is coming from somewhere other than the widget's palette, and I can't figure out how to change it to match the window's background.
edit:
This also has the rather unfortunate effect of messing up the colors when clicking the button multiple times:

So unless there's a way to mitigate that, unfortunately
Qt::WA_OpaquePaintEventon its own doesn't work. -
Can you test that again with the latest version of Qt ? Currently it's 6.4.1.
-
@SGaist I could but the project I'm working on is locked at Qt 6.2.3 (my company's decision, not mine) so I need a solution that works with that.
Currently I've settled for changing the button's style from mac to fusion, which presents a good enough version of a dark-colored button for my purposes, even if it doesn't match native buttons.
-
Even if you are locked to an older version, building with a recent one will let you know whether that issue has been fixed in between.
-
Then you should check the bug report system to see if there's already something about it there. If so, add informations on the ticket to help pinpoint the issue and if not, open a new one providing your minimal project there so it can be reproduced more easily.