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. [Solved] Nesting QtObject in QtQuick
QtWS25 Last Chance

[Solved] Nesting QtObject in QtQuick

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 3.6k 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.
  • G Offline
    G Offline
    Gennon
    wrote on last edited by
    #1

    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
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      -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
      0
      • G Offline
        G Offline
        Gennon
        wrote on last edited by
        #3

        [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
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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
          0
          • T Offline
            T Offline
            tomma
            wrote on last edited by
            #5

            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
            1
            • G Offline
              G Offline
              Gennon
              wrote on last edited by
              #6

              @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
              0
              • J Offline
                J Offline
                jtaylor
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jtaylor
                  wrote on last edited by jtaylor
                  #8

                  The solution without the aliases, or Items, is definitely cleaner and more memory efficient. Unfortunately, Creator can't understand the syntax and provide autocomplete on the properties within the nestings! Using the Item / alias pattern resolves that.

                  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