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. Editing C++ properties from QML with updates from C++

Editing C++ properties from QML with updates from C++

Scheduled Pinned Locked Moved QML and Qt Quick
qmlpropertybinding
3 Posts 2 Posters 1.1k 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.
  • S Offline
    S Offline
    swegmann
    wrote on last edited by
    #1

    I currently try to implement a custom QML control that allows me to change values of C++ object properties but also is updated when the C++ object property changes on its own. I wonder if there really is no other way to do this.

    So this is a working solution that I already have:

    MyClass.h

    Q_PROPERTY(int myCppValue READ myCppValue WRITE setMyCppValue NOTIFY myCppValueChanged)
    

    MyCustomControl.qml

    Item {
        property int value
    
        MouseArea {
            anchors.fill: parent
            onClicked: value = value + 1
        }
    }
    

    And now I instantiate this control as follows while assuming a MyClass instance is available as context property with the name "myclass":

    MyCustomControl {
        id: my_control
        value: myclass.myCppValue
    
        /* from here things get messy */
        Binding {
            target: myclass
            property: "myCppValue"
            value: my_control.value
        }
    
        Binding {
            target: my_control
            property: "value"
            value: myclass.myCppValue
        }
    }
    

    My problem seems to be, that I cannot create an alias property in MyCustomControl that actually reads and modifies the value myclass.myCppValue. As soon as MyCustomControl assigns a new value to "value" I loose my connection to the myclass property. So the only way I could circumvent this problem is to create two bindings that connect these two values and just assign myclass.myCppValue as property value of my_control to get a nice initial value.

    Is this the best you can do?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      richardo
      wrote on last edited by
      #2

      You could go the more imperative road, because now you have kind of endless update cycle:

          Connections {
                  target: myclass
                  onMyCppValueChanged: if (my_control.value !== myclass.myCppValue) my_control.value = myclass.myCppValue
          }
      
          Connections {
                  target: my_control
                  onValueChanged: if (my_control.value !== myclass.myCppValue) myclass.myCppValue = my_control.value
          }
      

      Then you would not have the cyclic update calles, that might lead to a problem.

      S 1 Reply Last reply
      0
      • R richardo

        You could go the more imperative road, because now you have kind of endless update cycle:

            Connections {
                    target: myclass
                    onMyCppValueChanged: if (my_control.value !== myclass.myCppValue) my_control.value = myclass.myCppValue
            }
        
            Connections {
                    target: my_control
                    onValueChanged: if (my_control.value !== myclass.myCppValue) myclass.myCppValue = my_control.value
            }
        

        Then you would not have the cyclic update calles, that might lead to a problem.

        S Offline
        S Offline
        swegmann
        wrote on last edited by
        #3

        @richardo I appreciate your input. Still I'm not sure I really like this approach that much better.

        As for the cycle, I couldn't see any of those usual cyclic property warnings that QML gives you in those cases. I think this cycle is already properly broken by using the correct construction in setMyCppValue:

        void setMyCppValue(int newValue)
        {
            if (newValue!=myCppValue)
            {
                myCppValue = newValue;
                emit myCppValueChanged();
            }
        }
        
        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