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. Possible to get style name in QML?
Qt 6.11 is out! See what's new in the release blog

Possible to get style name in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.6k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    Hi all -

    Actually, I know it's possible; I'm just looking for the easiest way to do it.

    Suppose I have an application that needs to run on multiple platforms, with different styles. In my main.cpp, I do this:

        QString qmlStyle = (platform == "android") ? "Material" : "Fusion";
        QQuickStyle::setStyle(qmlStyle);
    

    So, I know I can do this in a class, and declare a Q_PROPERTY, and all that stuff, but...I'm guessing there's an easier way to retrieve this stylename?

    I want to do this, so my QML can do something like this:

    TabBar {
        property string styleName // gotten from somewhere
        
        Component.onCompleted: {
            if (styleName == "Material") {
                Material.accent = Colors.primaryPurple
            } else if (styleName == "Fusion") {
                Fusion.accent = Colors.primaryPurple
            }
        }
    }
    

    which does seem like a hack, so if there's a better way to do this, I'm all ears.

    Thanks...

    1 Reply Last reply
    1
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      I would suggest a simple contextProperty:

       QString qmlStyle = (platform == "android") ? "Material" : "Fusion";
       QQuickStyle::setStyle(qmlStyle);
      
       QQmlApplicationEngine engine;
       engine.rootContext()->setContextProperty("StyleName", qmlStyle);
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      mzimmersM 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        I would suggest a simple contextProperty:

         QString qmlStyle = (platform == "android") ? "Material" : "Fusion";
         QQuickStyle::setStyle(qmlStyle);
        
         QQmlApplicationEngine engine;
         engine.rootContext()->setContextProperty("StyleName", qmlStyle);
        
        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @J-Hilk yeah, that is cleaner for sure...thanks. Can you see a better way to assign the accent based on the style than what I'm doing?

        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