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. Qml Shared Style
Qt 6.11 is out! See what's new in the release blog

Qml Shared Style

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 3.0k 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.
  • K Offline
    K Offline
    krobinson
    wrote on last edited by
    #1

    In my application I use a Qml Menu as a context menu in various places. I have created a MenuStyle that I would like to use on all of them without having to copy the style to each of them. I found the Qml Styling page https://wiki.qt.io/Qml_Styling. It seems to be close to what I want, but not quite.

    Style.qml

    pragma Singleton
    
    import QtQuick 2.4
    import QtQuick.Controls 1.4
    import QtQuick.Controls.Styles 1.4
    
    MenuStyle
    {
        id: contextMenuStyle
        frame: Rectangle { color: "gray" }
        itemDelegate.label: Label
                            {
                              anchors.horizontalCenter: parent.horizontalCenter
                              color: "white"
                              text: styleData.text
                            }
        itemDelegate.background: Rectangle { color: styleData.selected ? "blue" : "gray" }
    }
    

    Test.qml

    Menu
    {
        id: contextMenu
        style: MyStyle.contextMenuStyle;
        MenuItem
        {
    	    text: qsTr('Test')
        }
    }
    

    main.cpp

    qmlRegisterSingletonType(QUrl("file:///Qml/Style.qml"), "net.test.com", 1, 0, "MyStyle" );
    

    Can this be done this way or is there a better way?

    p3c0P 1 Reply Last reply
    0
    • X Offline
      X Offline
      xargs1
      wrote on last edited by xargs1
      #2

      I don't know if it can be done exactly like that, but the problem with that approach is you can only have one style in your Style.qml. My Style.qml typically has a QtObject as the root object, and properies within it for each property.

      pragma Singleton
      
      import QtQuick 2.3
      import QtQuick.Controls.Styles 1.1
      
      QtObject {
          property QtObject color: QtObject {
              readonly property color background: "#eeeeee"
              readonly property color button: "#60aabac5"
          }
          property var buttonStyle: Component {
              ButtonStyle {
      
              }
      }
      
      main.qml:
      
      Button {
          color: Style.color.button
          style: Style.buttonStyle
      }
      

      ...and you can put declare the singleton in your qmldir file, you don't have to write code to register it.

      K 1 Reply Last reply
      0
      • K krobinson

        In my application I use a Qml Menu as a context menu in various places. I have created a MenuStyle that I would like to use on all of them without having to copy the style to each of them. I found the Qml Styling page https://wiki.qt.io/Qml_Styling. It seems to be close to what I want, but not quite.

        Style.qml

        pragma Singleton
        
        import QtQuick 2.4
        import QtQuick.Controls 1.4
        import QtQuick.Controls.Styles 1.4
        
        MenuStyle
        {
            id: contextMenuStyle
            frame: Rectangle { color: "gray" }
            itemDelegate.label: Label
                                {
                                  anchors.horizontalCenter: parent.horizontalCenter
                                  color: "white"
                                  text: styleData.text
                                }
            itemDelegate.background: Rectangle { color: styleData.selected ? "blue" : "gray" }
        }
        

        Test.qml

        Menu
        {
            id: contextMenu
            style: MyStyle.contextMenuStyle;
            MenuItem
            {
        	    text: qsTr('Test')
            }
        }
        

        main.cpp

        qmlRegisterSingletonType(QUrl("file:///Qml/Style.qml"), "net.test.com", 1, 0, "MyStyle" );
        

        Can this be done this way or is there a better way?

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @krobinson

        qmlRegisterSingletonType(QUrl("file:///Qml/Style.qml"), "net.test.com", 1, 0, "MyStyle" );

        The first parameter uri is the one which you will use as an import in QML.
        Check this example.

        157

        1 Reply Last reply
        0
        • X xargs1

          I don't know if it can be done exactly like that, but the problem with that approach is you can only have one style in your Style.qml. My Style.qml typically has a QtObject as the root object, and properies within it for each property.

          pragma Singleton
          
          import QtQuick 2.3
          import QtQuick.Controls.Styles 1.1
          
          QtObject {
              property QtObject color: QtObject {
                  readonly property color background: "#eeeeee"
                  readonly property color button: "#60aabac5"
              }
              property var buttonStyle: Component {
                  ButtonStyle {
          
                  }
          }
          
          main.qml:
          
          Button {
              color: Style.color.button
              style: Style.buttonStyle
          }
          

          ...and you can put declare the singleton in your qmldir file, you don't have to write code to register it.

          K Offline
          K Offline
          krobinson
          wrote on last edited by krobinson
          #4

          @xargs1 You have done the style this way before and it worked?

          I switched to this and it is still not working. Not sure if the qmldir is setup right.

          My directory structure looks like this

          • absolute
            | + Qml
            | | + qmldir
            | | + Style.qml
            | | + MainWindow.qml
            | | + main.qml

          Style.qml

          pragma Singleton
          
          import QtQuick 2.4
          import QtQuick.Controls 1.4
          import QtQuick.Controls.Styles 1.4
          
          QtObject
          {
          property var contextMenuStyle: Component
                                         {
                                              MenuStyle
                                              {
                                                  frame: Rectangle { color: "gray" }
                                                  itemDelegate.label: Label
                                                                      {
                                                                        anchors.horizontalCenter: parent.horizontalCenter
                                                                        color: "white"
                                                                        text: styleData.text
                                                                      }
                                                  itemDelegate.background: Rectangle { color: styleData.selected ? "blue" : "gray" }
                                              }
                                         }
          }
          

          MainWindow.qml

          Menu
          {
              id: contextMenu
              style: MyStyle.contextMenuStyle;
              
              MenuItem
              {
                  text: qsTr('Test')
              }
          }
          

          qmldir

          singleton MyStyle 1.0 Style.qml
          
          1 Reply Last reply
          0
          • K Offline
            K Offline
            krobinson
            wrote on last edited by
            #5

            Ok, I finally got it. I forgot to take the code out of main.cpp once I added the qmldir. Thanks for the suggestions.

            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