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. About accessing a component's root level properties
Qt 6.11 is out! See what's new in the release blog

About accessing a component's root level properties

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 1.0k 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.
  • Stan HuangS Offline
    Stan HuangS Offline
    Stan Huang
    wrote on last edited by
    #1

    Below is a QML module, ClickableImage.qml:

    // ClickableImage.qml
    // Simple image which can be clicked
    import QtQuick 2.5
    Image {
    id: root
    signal clicked
    MouseArea {
    anchors.fill: parent
    onClicked: root.clicked()
    }
    }

    Then, another module calling this ClickableImage:

    import QtQuick 2.5
    Item {
    // set width based on given background
    width: bg.width
    height: bg.height
    Image { // nice background image
    id: bg
    source: "assets/background.png"
    }
    MouseArea {
    .....
    }
    ClickableImage {
    id: circle
    x: 84; y: 68
    source: "assets/circle_blue.png"
    antialiasing: true
    onClicked: {
    // increase the x-position on click
    x += 20
    }
    }
    ....
    }

    But I found a section of text "It is important to know, that only the root
    level properties can be accessed from outside this file by other components."

    I found no property defined at ClickableImage.qml which can be accessed at Transportation.qml. Besides, in Transportation.qml, inside ClickableImage, the properties, such as 'x', 'y' and 'source' are not defined at root level of Image at ClickableImage.qml.
    How come?

    J.HilkJ 1 Reply Last reply
    0
    • CKurduC Offline
      CKurduC Offline
      CKurdu
      wrote on last edited by
      #2

      @Stan-Huang said in About accessing a component's root level properties:

      ClickableImage

      You can use "property alias" feature to open ClickableImage props to outside.

      //Transportation.qml
      import QtQuick 2.5
      Item {
              id:root
              property alias ci_x: circle.x
              property alias ci_y: circle.y
              property alias ci_source: circle.source
              // set width based on given background
              width: bg.width
              height: bg.height
              Image {
                  // nice background image
                  id: bg
                  source: "assets/background.png"
                  MouseArea {
                  }
              }
              ClickableImage {
                          id: circle
                          x: 84
                          y: 68
                          source: "assets/circle_blue.png"
                          antialiasing: true
                          onClicked: {
                              // increase the x-position on click
                              x += 20
                          }
                      }
      
      }
      
      

      You reap what you sow it

      1 Reply Last reply
      0
      • Stan HuangS Stan Huang

        Below is a QML module, ClickableImage.qml:

        // ClickableImage.qml
        // Simple image which can be clicked
        import QtQuick 2.5
        Image {
        id: root
        signal clicked
        MouseArea {
        anchors.fill: parent
        onClicked: root.clicked()
        }
        }

        Then, another module calling this ClickableImage:

        import QtQuick 2.5
        Item {
        // set width based on given background
        width: bg.width
        height: bg.height
        Image { // nice background image
        id: bg
        source: "assets/background.png"
        }
        MouseArea {
        .....
        }
        ClickableImage {
        id: circle
        x: 84; y: 68
        source: "assets/circle_blue.png"
        antialiasing: true
        onClicked: {
        // increase the x-position on click
        x += 20
        }
        }
        ....
        }

        But I found a section of text "It is important to know, that only the root
        level properties can be accessed from outside this file by other components."

        I found no property defined at ClickableImage.qml which can be accessed at Transportation.qml. Besides, in Transportation.qml, inside ClickableImage, the properties, such as 'x', 'y' and 'source' are not defined at root level of Image at ClickableImage.qml.
        How come?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Stan-Huang said in About accessing a component's root level properties:

        But I found a section of text "It is important to know, that only the root
        level properties can be accessed from outside this file by other components."
        I found no property defined at ClickableImage.qml which can be accessed at Transportation.qml. Besides, in Transportation.qml, inside ClickableImage, the properties, such as 'x', 'y' and 'source' are not defined at root level of Image at ClickableImage.qml.
        How come?

        The root item itself has a bunch of properties that can be accessed from outside.
        The root item of ClickableImage is a Image therefore all properties of image are also accessible for ClickableImage.

        You can see what properties a QML Item has, from the documentation:
        https://doc.qt.io/qt-5/qml-qtquick-image.html

        and since Image inherits from Item as well, are all Item properties also part of the Image
        https://doc.qt.io/qt-5/qml-qtquick-item.html


        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.

        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