Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] Nesting QtObject in QtQuick

    QML and Qt Quick
    3
    6
    2790
    Loading More Posts
    • 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.
    • G
      Gennon last edited by

      Hi,

      I was looking at http://qt-project.org/wiki/QmlStyling and got an idea to nest "QtObjects":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-item.html so that I could do something like this:

      @// Style.qml
      QtObject {
      property alias text: text
      property alias window: window

      QtObject{
            id: text
            property color normal: "black"
            property color disabled: "gray"
      }
      
      QtObject{
            id: window
            property color background: "white"
      }
      

      }

      // root component
      Rectangle {
      ...
      Style { id: style }
      ...
      }

      // in use
      Text {
      color: style.text.normal
      text: "Hello World"
      }@

      But it turns out that QtObject does not have any children property and thus can't be nested this way. On the other hand you might say, use "Item":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-item.html instead. But Item is more for visual object and I want a lightweight module like QtObject.

      So my question; is there any other QtQuick modules I could use? Or do I have to make my own? Perhaps there is a neat trick to do this?

      Solution:
      [quote author="Tomma" date="1372162774"]You can fix that by using those QtObjects as properties without aliasing.
      [/quote]

      @QtObject {
      property QtObject text: QtObject{
      property color normal: "black"
      property color disabled: "gray"
      }
      property QtObject window: QtObject{
      property color background: "white"
      }
      }@

      /Gen

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        -That would not work anyway because internal objects of a file you are including are not exposed to the user ("window" and "text" objects are not visible to QML code in your root component or in use).-

        EDIT: sorry I've obviously misread your post ;) I somehow missed the "alias" part.

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • G
          Gennon last edited by

          [quote author="sierdzio" date="1372161354"]I somehow missed the "alias" part.[/quote]

          The alias works like a charm. I can change all the root objects to be Item instead of QtObject, but then I will get a conflict if I have property names like width or height etc. And those are names I might use.

          /Gen

          1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators last edited by

            What you could do instead is to leverage JSON (quite easy and should work) or attached properties (hardcore, haven't done this myself).
            @
            QtObject {
            property variant text: { "normal": "black", "disabled": "gray" }
            // In QtQuick 2.0, better use 'var' instead of 'variant'
            }

            // use it!
            Text {
            color: style.text.normal
            text: "Hello World"
            }
            @

            (Z(:^

            1 Reply Last reply Reply Quote 0
            • T
              tomma last edited by

              You can fix that by using those QtObjects as properties without aliasing.
              @QtObject {
              property QtObject text: QtObject{
              property color normal: "black"
              property color disabled: "gray"
              }
              property QtObject window: QtObject{
              property color background: "white"
              }
              }@

              1 Reply Last reply Reply Quote 1
              • G
                Gennon last edited by

                @sierdzio: Thanks, I was trying that as well, but then I had to parse int values and did not get the color helper from Qt Crator when using colors. I might have done something wrong, but I felt it was not the most correct way of doing things.

                @Tomma: That actually solves my problem! Thanks! I knew there was a simple solution!

                /Gen

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post