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. Qt 6.8.1 setting Color Scheme (Dark Mode) does not update Button colors
QtWS25 Last Chance

Qt 6.8.1 setting Color Scheme (Dark Mode) does not update Button colors

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 6.8.1mingw 13.1.0windows 11
7 Posts 2 Posters 539 Views
  • 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.
  • K Offline
    K Offline
    KK2022
    wrote on last edited by
    #1

    Hello everyone!
    I have a project (MinGW (13.1.0) 64 Bit - Qt 6.8.1 - Windows 11) where the application starts with a few push buttons disabled during start. Only after some conditions are met are the buttons enabled.
    I am trying to add a check box which enables dark or light mode changes.
    The problem is when the application starts in dark mode and the buttons are disabled in the beginning, the buttons are greyed out and the text is white inside the buttons.
    When I try changing from dark to light mode, the text is not visible at all since the text color remains the same. Is there a better way to make sure that the system updates the colors after setting the new color scheme, instead of manually changing the color of the text inside the push buttons?
    I tried qApp->processEvents() and it unfortunately didn't help either.

    I wrote a simple project to test this issue. MainWindow.cpp file for reference

    #include "mainwindow.hpp"
    #include "./ui_mainwindow.h"
    
    #include <QStyleHints>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        // Setting the application to dark mode to see the issue
        ui->checkBoxDarkMode->setCheckState(Qt::CheckState::Checked);
    
        connect(ui->checkBoxEnableButtons, &QCheckBox::checkStateChanged, this, &MainWindow::toggleButtonActivation);
        connect(ui->checkBoxDarkMode, &QCheckBox::checkStateChanged, this, &MainWindow::darkModeEnable);
    
        toggleButtonActivation(ui->checkBoxEnableButtons->checkState());
        darkModeEnable(ui->checkBoxDarkMode->checkState());
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::toggleButtonActivation(Qt::CheckState state)
    {
        for (auto button : ui->centralwidget->findChildren<QPushButton*>())
        {
            if(state == Qt::CheckState::Checked)
                button->setEnabled(true);
            else if(state == Qt::CheckState::Unchecked)
                button->setEnabled(false);
        }
    }
    
    void MainWindow::darkModeEnable(Qt::CheckState state)
    {
        QStyleHints *styleHints = QGuiApplication::styleHints();
    
        switch(state)
        {
        case Qt::CheckState::Checked:
            styleHints->setColorScheme(Qt::ColorScheme::Dark);
            // qApp->processEvents();
            break;
        case Qt::CheckState::Unchecked:
            styleHints->setColorScheme(Qt::ColorScheme::Light);
            // qApp->processEvents();
            break;
        default:
            break;
        }
    }
    
    

    Some pictures for refence.
    If the application starts with dark mode :
    0e52540f-54b6-4e98-9691-1490bbbaecc4-image.png
    8b017289-4ed4-4ead-ab5c-485fffda79a3-image.png
    5c8b473b-7def-41e1-b6c7-269dac80d970-image.png

    If the application starts with light mode :
    4a705b23-b52b-4cdd-a0fe-fd026d004d43-image.png
    88e46320-54c6-472c-9185-8787d3ce915b-image.png
    8200dcda-0b80-4fbd-afeb-baf6502e5344-image.png

    Any help is appreciated! Thank you.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please provide a minimal, compileable example without a ui file so we can take a look on it.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KK2022
        wrote on last edited by
        #3

        I am not aware of a pastebin for multiple files.

        Here I have attached the CMakeLists.txt, main.cpp, MainWindow.hpp, MainWindow.cpp and MainWindow.ui on the limewire site that I can share.
        https://limewire.com/d/e8193dc6-0c30-4a0e-914c-750d8638e966#rRQQxJ02XcOaL7h-EmUquBVEl1bmlL2qE3k5b5fe6nc

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KK2022
          wrote on last edited by
          #4

          Pastebin for each individual file

          CMakeLists.txt - https://pastebin.com/k9xAsL5e
          mainwindow.hpp - https://pastebin.com/NrK2DXW5
          mainwindow.ui - https://pastebin.com/6k5EtrvM

          mainwindow.cpp is already available on the question.

          main.cpp is as follows

          #include "mainwindow.hpp"
          
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
              return a.exec();
          }
          
          
          1 Reply Last reply
          0
          • K Offline
            K Offline
            KK2022
            wrote on last edited by
            #5

            Any suggestions guys? Any help would be great.

            Christian EhrlicherC 1 Reply Last reply
            0
            • K KK2022

              Any suggestions guys? Any help would be great.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @KK2022 said in Qt 6.8.1 setting Color Scheme (Dark Mode) does not update Button colors:

              Any help would be great.

              As I said - write a reproduce. This can be done in less than 50 lines - just some pushbuttons and a switch to toggle between dark/light mode.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              K 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @KK2022 said in Qt 6.8.1 setting Color Scheme (Dark Mode) does not update Button colors:

                Any help would be great.

                As I said - write a reproduce. This can be done in less than 50 lines - just some pushbuttons and a switch to toggle between dark/light mode.

                K Offline
                K Offline
                KK2022
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Here is a quickly written piece of code to reproduce the issue. It is just a single main.cpp file. I have to use pastebin since my message is being flagged as spam (I have no idea why!)
                https://pastebin.com/DLVbDLzj

                It depends on which mode the system starts with. I have the issue when the system starts with dark mode.

                1 Reply Last reply
                1

                • Login

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