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. Switching DesktopSettingsAware colors for a button
Qt 6.11 is out! See what's new in the release blog

Switching DesktopSettingsAware colors for a button

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 361 Views 1 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.
  • C Offline
    C Offline
    cadol001
    wrote on last edited by
    #1

    If I have a button where it's default colors are set through the Desktop settings, can I switch out two of those colors?

    For example:

        QPushButton* button = new QPushButton("hello world",this);
        QPalette pal = button->palette();
        pal.setColor(QPalette::Button, QPalette::Base);
        button->setAutoFillBackground(true);
        button->setPalette(pal);
        button->update();
    

    In this code I'm setting the button color to QPalette::Base. This works as long as I restart the app whenever settings change, but I'd like to just specify to use the new QPalette::Base during runtime whenever the color changes. ie. switching from light theme to dark theme changes the default QPalette::Base color, but it only updates buttons that don't have their palettes changed.

    How can I accomplish this?

    Thanks

    T 1 Reply Last reply
    0
    • C cadol001

      If I have a button where it's default colors are set through the Desktop settings, can I switch out two of those colors?

      For example:

          QPushButton* button = new QPushButton("hello world",this);
          QPalette pal = button->palette();
          pal.setColor(QPalette::Button, QPalette::Base);
          button->setAutoFillBackground(true);
          button->setPalette(pal);
          button->update();
      

      In this code I'm setting the button color to QPalette::Base. This works as long as I restart the app whenever settings change, but I'd like to just specify to use the new QPalette::Base during runtime whenever the color changes. ie. switching from light theme to dark theme changes the default QPalette::Base color, but it only updates buttons that don't have their palettes changed.

      How can I accomplish this?

      Thanks

      T Offline
      T Offline
      Tink
      wrote on last edited by Tink
      #2

      @cadol001 try this:

      void MainWindow::changeEvent(QEvent *event)
      {
          if (event->type() == QEvent::PaletteChange) {
             QPalette palette = QApplication::style()->standardPalette();
             palette.setColor(QPalette::Button, palette.color(QPalette::Base));
             button->setPalette(palette);
          }
       event->ignore();
      }
      
      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