Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there any way to get QPushButtons to render as fully opaque in macOS dark mode?

Is there any way to get QPushButtons to render as fully opaque in macOS dark mode?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 992 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Guy GizmoG Offline
    Guy GizmoG Offline
    Guy Gizmo
    wrote on last edited by
    #1

    I'm updating an old app to properly support dark mode in macOS, and in one case a QPushButton gets 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:

    Screen Shot 2023-01-02 at 1.42.30 PM.png

    This is no good! I need the button to be opaque, but still render as native style macOS button. So I can't use QPalette as 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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Guy GizmoG 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        Guy GizmoG Offline
        Guy GizmoG Offline
        Guy Gizmo
        wrote on last edited by
        #3

        @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:

        Screen Shot 2023-01-02 at 2.01.23 PM.png

        How it looks in dark mode in 10.14:

        Screen Shot 2023-01-02 at 2.03.28 PM.png

        And the same issue is present in macOS 12:

        Screen Shot 2023-01-02 at 2.04.47 PM.png

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gvanvoor
          wrote on last edited by
          #4

          Have yout tried setting the Qt::WA_OpaquePaintEvent attribute on the button?

          Guy GizmoG 1 Reply Last reply
          0
          • G gvanvoor

            Have yout tried setting the Qt::WA_OpaquePaintEvent attribute on the button?

            Guy GizmoG Offline
            Guy GizmoG Offline
            Guy Gizmo
            wrote on last edited by Guy Gizmo
            #5

            @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:

            Screen Shot 2023-01-03 at 9.27.03 AM.png

            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:

            bad-colors.gif

            So unless there's a way to mitigate that, unfortunately Qt::WA_OpaquePaintEvent on its own doesn't work.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you test that again with the latest version of Qt ? Currently it's 6.4.1.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Guy GizmoG 2 Replies Last reply
              0
              • SGaistS SGaist

                Can you test that again with the latest version of Qt ? Currently it's 6.4.1.

                Guy GizmoG Offline
                Guy GizmoG Offline
                Guy Gizmo
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Can you test that again with the latest version of Qt ? Currently it's 6.4.1.

                    Guy GizmoG Offline
                    Guy GizmoG Offline
                    Guy Gizmo
                    wrote on last edited by
                    #9

                    @SGaist I just tried with Qt 6.4.2 and it's still affected by these two issues.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved