Guest User

Qt 6.8.1 Dark Mode button text colors issue

a guest
Jan 24th, 2025
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | Software | 0 0
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QPushButton>
  4. #include <QCheckBox>
  5. #include <QVBoxLayout>
  6. #include <QStyleHints>
  7.  
  8. #define DARK_MODE Qt::CheckState::Checked
  9. #define LIGHT_MODE Qt::CheckState::Unchecked
  10. #define BUTTONS_ENABLED Qt::CheckState::Checked
  11. #define BUTTONS_DISABLED Qt::CheckState::Unchecked
  12.  
  13. QPushButton *button1;
  14. QPushButton *button2;
  15.  
  16. void toggleButtonActivation(Qt::CheckState state)
  17. {
  18.     // If the state is checked, disable the buttons
  19.     if (state == Qt::CheckState::Checked)
  20.     {
  21.         button1->setEnabled(true);
  22.         button2->setEnabled(true);
  23.     }
  24.     else
  25.     {
  26.         button1->setEnabled(false);
  27.         button2->setEnabled(false);
  28.     }
  29. }
  30.  
  31. void darkModeEnable(Qt::CheckState state)
  32. {
  33.     QStyleHints *styleHints = QGuiApplication::styleHints();
  34.  
  35.     if (state == Qt::CheckState::Checked)
  36.     {
  37.         styleHints->setColorScheme(Qt::ColorScheme::Dark);
  38.     }
  39.     else
  40.     {
  41.         styleHints->setColorScheme(Qt::ColorScheme::Light);
  42.     }
  43. }
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47.     QApplication a(argc, argv);
  48.  
  49.     // Here is the issue. If the system starts with light mode, it isn't much of a problem.
  50.     // However, if the system starts with dark mode, the text on the buttons (if disabled) isn't visible anymore when switching to light mode.
  51.     darkModeEnable(LIGHT_MODE); // Mode during start
  52.  
  53.     QWidget window;
  54.     window.resize(320, 240);
  55.  
  56.     QCheckBox * checkBoxEnableButtons = new QCheckBox("Enable Buttons", &window);
  57.     button1 = new QPushButton("Button 1", &window);
  58.     button2 = new QPushButton("Button 2", &window);
  59.     QCheckBox *checkBoxDarkMode = new QCheckBox("Dark Mode", &window);
  60.  
  61.     QVBoxLayout *layout = new QVBoxLayout;
  62.     layout->addWidget(checkBoxEnableButtons);
  63.     layout->addWidget(button1);
  64.     layout->addWidget(button2);
  65.     layout->addWidget(checkBoxDarkMode);
  66.     window.setLayout(layout);
  67.  
  68.     toggleButtonActivation(BUTTONS_DISABLED);
  69.     checkBoxEnableButtons->setCheckState(BUTTONS_DISABLED);
  70.  
  71.     QObject::connect(checkBoxEnableButtons, &QCheckBox::checkStateChanged, &toggleButtonActivation);
  72.     QObject::connect(checkBoxDarkMode, &QCheckBox::checkStateChanged, &darkModeEnable);
  73.  
  74.     window.show();
  75.  
  76.     return a.exec();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment