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 ?
Forum Updated to NodeBB v4.3 + New Features

How to get current theme color ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
themecolorqml
10 Posts 4 Posters 4.2k Views 3 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.
  • P Offline
    P Offline
    pamplemousse_mk2
    wrote on 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
    1
    • SeeLookS Offline
      SeeLookS Offline
      SeeLook
      wrote on last edited by SeeLook
      #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
      1
      • SeeLookS SeeLook

        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 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
        • SeeLookS Offline
          SeeLookS Offline
          SeeLook
          wrote on last edited by
          #4

          Under Linux SystemPalete works out of the box.

          RokeJulianLockhartR 1 Reply Last reply
          1
          • SeeLookS SeeLook

            Under Linux SystemPalete works out of the box.

            RokeJulianLockhartR Offline
            RokeJulianLockhartR Offline
            RokeJulianLockhart
            wrote on last edited by RokeJulianLockhart
            #5

            Under Linux, SystemPalete works out of the box.

            @SeeLook, is it accessible from outside QML?

            You won't find Windows accent color this way.

            That seems strange. Does a JIRA FR for this exist?

            When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QtTester
              wrote last edited by
              #6

              I think we need to write a function:
              function accentColor()
              {
              if(theme == "Material")
              return Material.accent;
              else
              return IOS.accent;
              }

              RokeJulianLockhartR 1 Reply Last reply
              0
              • Q QtTester

                I think we need to write a function:
                function accentColor()
                {
                if(theme == "Material")
                return Material.accent;
                else
                return IOS.accent;
                }

                RokeJulianLockhartR Offline
                RokeJulianLockhartR Offline
                RokeJulianLockhart
                wrote last edited by
                #7

                @QtTester, by IOS, do you mean qt.io/blog/qt-quick-controls-2-ios-style? Regardless, post/744511 corroborates this.

                When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                1 Reply Last reply
                0
                • SeeLookS Offline
                  SeeLookS Offline
                  SeeLook
                  wrote last edited by SeeLook
                  #8

                  Hi all,

                  Seems like all that above was cleared out with Qt 6. At least looking from Qt 6.9 point of view.
                  QGuiApplication::palette() keeps system colors either for Android and Windows now (I'm not able to confirm that for MacOS).
                  Which means either Windows accent color and Android (14+) accent color is QGuiApplication::palette() just out of the box.
                  The same with light/dark stuff. All is inside.
                  And this C++ QPalette is SystemPalette under QML.

                  Still, If one want to mess up with QML Material accent color, just add something like below to the main *.qml file:

                  import QtQuick.Controls.Material
                  
                  // ...
                  
                  SystemPalette {
                      id: activePalette
                      colorGroup: SystemPalette.Active
                  }
                  Material.accent: activePalette.highlight
                  

                  And similar applies to Mac style, I guess.

                  Q 1 Reply Last reply
                  1
                  • SeeLookS SeeLook

                    Hi all,

                    Seems like all that above was cleared out with Qt 6. At least looking from Qt 6.9 point of view.
                    QGuiApplication::palette() keeps system colors either for Android and Windows now (I'm not able to confirm that for MacOS).
                    Which means either Windows accent color and Android (14+) accent color is QGuiApplication::palette() just out of the box.
                    The same with light/dark stuff. All is inside.
                    And this C++ QPalette is SystemPalette under QML.

                    Still, If one want to mess up with QML Material accent color, just add something like below to the main *.qml file:

                    import QtQuick.Controls.Material
                    
                    // ...
                    
                    SystemPalette {
                        id: activePalette
                        colorGroup: SystemPalette.Active
                    }
                    Material.accent: activePalette.highlight
                    

                    And similar applies to Mac style, I guess.

                    Q Offline
                    Q Offline
                    QtTester
                    wrote last edited by
                    #9

                    @SeeLook
                    Hi, i think you misunderstood the meaning. he just want to get a color from different style(material or ios), not from current windows's style.

                    RokeJulianLockhartR 1 Reply Last reply
                    0
                    • Q QtTester

                      @SeeLook
                      Hi, i think you misunderstood the meaning. he just want to get a color from different style(material or ios), not from current windows's style.

                      RokeJulianLockhartR Offline
                      RokeJulianLockhartR Offline
                      RokeJulianLockhart
                      wrote last edited by
                      #10

                      @QtTester, I think I'd misunderstood, too... Thanks.

                      When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                      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