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. Private properties in QML
Forum Updated to NodeBB v4.3 + New Features

Private properties in QML

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 8.4k Views 1 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.
  • Z Offline
    Z Offline
    Zeiko
    wrote on last edited by
    #1

    It seems like all instanciated objects in QML inherits all properties from the object it has been instanciated from.
    What if you want a property in a parent that should not be accessible from a child?
    If your creating a library for example that you don't want to be able to access everything from your main application?

    I've tried using this example to make properties private but it doesen't work, was it only prior to qt 5 it worked?
    https://qt.gitorious.org/qt-components/pages/CodingStyle
    @
    ////main.qml

    Rectangle {

    Item {
    QtObject {
    id: internal
    property int shouldBeInvisibleOutsideThisQmlFile: 1
    }
    }

    Test {

    }

    }

    ////Test.qml

    Rectangle {

    width: internal.shouldBeInvisibleOutsideThisQmlFile; //this works but shouldn't

    }
    @

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      This works the other way around. Parents can't access properties of children that aren't in the top level item. Children can access properties of parents.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        Zeiko
        wrote on last edited by
        #3

        Yes I know but shouldn't it be possible to make a property not accessable from child items?

        Lets say you want a property called dataContext on each view in QML if you have a childview that also have a property dataContext this will point to the partent dataContext property. It should be convenient to somehow be able to make a property private and only visible from one QML-file.

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          I'm not sure what the view being referred to is. Is this an observed or a speculative problem?

          A property in a child will hide property with the same name in the parent.

          main.qml
          @
          import QtQuick 2.2

          Rectangle {
          width: 360
          height: 360

          property string myProperty: "parent"
          
          MouseArea {
              anchors.fill: parent
              onClicked: console.log("parent: " + myProperty)
          }
          
          Child {}
          

          }

          @

          Child.qml
          @import QtQuick 2.0

          Rectangle {
          width: 100
          height: 100

          color: "red"
          property string myProperty: "Child"
          MouseArea {
              anchors.fill: parent
              onClicked: console.log("Child: " + myProperty)
          }
          
          Rectangle {
              property string myProperty: "sub-Child"
              width: 50
              height: 50
              color: "green"
              MouseArea {
                  anchors.fill: parent
                  onClicked: console.log("sub-Child: " + myProperty)
              }
          }
          

          }
          @

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            Zeiko
            wrote on last edited by
            #5

            Thx for the feedback.

            I have a tabview with different QML-pages in each tab.
            Its possible to workaround but lets say you want to set a property in a child that has the same name as a property in the parent,
            that wont work.

            @//Main.qml (parent)
            import QtQuick 2.2

            Rectangle {
            width: 360
            height: 360

            property CppClass1 dataContext: CppClass1{}
            
            Child {
               dataContext: dataContext.childDataContext //This won't work because dataContext is always refering to the one in this qml file. Any ideas except changing the name of dataContext?
            }
            

            }

            //Child.qml
            import QtQuick 2.0

            Rectangle {
            width: 100
            height: 100

            property CppClass2 dataContext: CppClass2{}

            }@

            1 Reply Last reply
            0
            • jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              How about giving the Child{} an id, and then using that to refer to the child's dataContext property? The same idea can be applied to accessing the parent, or by use of an explicit parent.

              ie (compressed for brevity):

              @
              Rectangle {
              id: parentItem
              property variant myProperty

              Child {
                  id: childItem
                  property variant myProperty 
                  // use parentItem.myProperty or parent.myProperty
                  //if the parent is the intended source
                  property variant targetProperty: childItem.myProperty
              }
              

              }
              @

              Changing the name of a property for the sake of clarity isn't a bad option, when the development process allows for it.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zeiko
                wrote on last edited by
                #7

                thx for the tip

                dataContext: dataContext.childDataContext
                this line was actually referering to the dataContext in the child.qml file all the time. I changed it to this and now it works :)

                @//Main.qml (parent)
                import QtQuick 2.2

                Rectangle {
                id: mainPage
                width: 360
                height: 360

                property CppClass1 dataContext: CppClass1{}
                
                Child {
                   dataContext: mainPage.dataContext.childDataContext 
                }
                

                }

                //Child.qml
                import QtQuick 2.0

                Rectangle {
                width: 100
                height: 100

                property CppClass2 dataContext: CppClass2{}

                }@

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  Zeiko
                  wrote on last edited by
                  #8

                  However "mainPage.dataContext.childDataContext" can then be called from the rest of the appliaction. Not a big issue but would be nice to make the parent dataContext private (only available in main.qml) somehow.

                  1 Reply Last reply
                  0
                  • jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote on last edited by
                    #9

                    I agree that enforcing a cleaner interface would be nice. As is, property use takes a little more discipline

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    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