Qt 6.8.1 setting Color Scheme (Dark Mode) does not update Button colors
-
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 triedqApp->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 :
If the application starts with light mode :
Any help is appreciated! Thank you.
-
Please provide a minimal, compileable example without a ui file so we can take a look on it.
-
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 -
Pastebin for each individual file
CMakeLists.txt - https://pastebin.com/k9xAsL5e
mainwindow.hpp - https://pastebin.com/NrK2DXW5
mainwindow.ui - https://pastebin.com/6k5EtrvMmainwindow.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(); }
-
@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.
-
@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/DLVbDLzjIt depends on which mode the system starts with. I have the issue when the system starts with dark mode.