Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get current theme color ?
QtWS25 Last Chance

How to get current theme color ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
themecolorqml
4 Posts 2 Posters 3.0k 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.
  • P Offline
    P Offline
    pamplemousse_mk2
    wrote on 4 Mar 2021, 09:58 last edited by
    #1

    Hello,

    Is there a way to make my QML code be responsive to the colors of the current theme ?

    import QtQuick 2.0
    import QtQuick.Controls 2.0
    import QtQuick.Controls.Material 2.12
    
    Button {
      checkable: true
      autoExclusive: true
    
      Rectangle {
        width: 12
        height: width
        x: 0
        y: 5
        color: Material.accent
        border.color: "black"
        border.width: 1
        radius: width*0.5
      }
    }
    

    Here in my code, I used explicitly Material.accent to set the color of the rectangle, but if I change the theme, that value will be invalid. Is there a global property to get the current accent color, in example theme.accent or qtquicktheme.accent to make my code independant from the Material theme ? I can't find such information in the Qt documentation.

    Regards.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SeeLook
      wrote on 4 Mar 2021, 12:41 last edited by SeeLook 3 Apr 2021, 12:44
      #2

      Hi,

      You might use SystemPalette:

      SystemPalette {
          id: activePalette
          colorGroup: SystemPalette.Active
        }
      

      then use it somewhere:
      color: activePalette.highlight
      or any of its property that suits your need.

      But You won't find Windows accent color this way. To do so, some registry key has to be read:

      Here is example how to change highlight color with Windows accent color and use it in application palette. (usually done in main.cpp before loading QML).

        QSettings accent(QStringLiteral("HKEY_USERS\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent"),
                           QSettings::NativeFormat);
          if (accent.contains(QLatin1String("StartColorMenu"))) {
            int color = accent.value(QLatin1String("StartColorMenu")).toInt();
            int r = color & 0xff;
            int g = (color >> 8) & 0xff;
            int b = (color >> 16) & 0xff;
            auto pal = qApp->palette();
            QColor c(r, g, b);
            pal.setColor(QPalette::Active, QPalette::Highlight, c.lighter(150)); 
          // Accent color is quite strong , so here it is made lighter
            qApp->setPalette(pal);
          }
      

      But how to get some system color under Android (if there is any) I even don't know.

      P 1 Reply Last reply 8 Mar 2021, 14:37
      0
      • S SeeLook
        4 Mar 2021, 12:41

        Hi,

        You might use SystemPalette:

        SystemPalette {
            id: activePalette
            colorGroup: SystemPalette.Active
          }
        

        then use it somewhere:
        color: activePalette.highlight
        or any of its property that suits your need.

        But You won't find Windows accent color this way. To do so, some registry key has to be read:

        Here is example how to change highlight color with Windows accent color and use it in application palette. (usually done in main.cpp before loading QML).

          QSettings accent(QStringLiteral("HKEY_USERS\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent"),
                             QSettings::NativeFormat);
            if (accent.contains(QLatin1String("StartColorMenu"))) {
              int color = accent.value(QLatin1String("StartColorMenu")).toInt();
              int r = color & 0xff;
              int g = (color >> 8) & 0xff;
              int b = (color >> 16) & 0xff;
              auto pal = qApp->palette();
              QColor c(r, g, b);
              pal.setColor(QPalette::Active, QPalette::Highlight, c.lighter(150)); 
            // Accent color is quite strong , so here it is made lighter
              qApp->setPalette(pal);
            }
        

        But how to get some system color under Android (if there is any) I even don't know.

        P Offline
        P Offline
        pamplemousse_mk2
        wrote on 8 Mar 2021, 14:37 last edited by
        #3

        @SeeLook Hello, thank you for your answer. I will keep in mind SystemPalette for other use. Unfortunately, I work on Linux. Perhaps is there no solution and I must stick with Material.accent.

        Regards.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SeeLook
          wrote on 8 Mar 2021, 14:55 last edited by
          #4

          Under Linux SystemPalete works out of the box.

          1 Reply Last reply
          0

          2/4

          4 Mar 2021, 12:41

          • Login

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