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. Binding does not update property but Connections object does
Qt 6.11 is out! See what's new in the release blog

Binding does not update property but Connections object does

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

    There must be something I am missing about how the Qml property binding system works. The following code does not update the Text.text property when the notify signal for either of the properties personAName and personBName are changed:

    Text {
        id: titleLabel
        text: "%1, %2".arg(marriageModel.personAName).arg(marriageModel.personBName)
    }
    

    However, using a connection object does work:

    Text {
        id: titleLabel
        function updateText() {
            this.text = "%1, %2".arg(marriageModel.personAName).arg(marriageModel.personBName)
            print(text)
        }
        Connections {
            target: marriageModel
            onPersonANameChanged: titleLabel.updateText()
            onPersonBNameChanged: titleLabel.updateText()
        }
    }
    

    Surely these two cases are functionally equivalent according to the Qml spec? What are the conditions under which the Text.text in the first example wouldn't update when personANameChanged and personBNameChanged signals are emitted, but it is updated with explicit Connections to the signals in the second example?

    Thanks!

    https://alaskafamilysystems.com/

    1 Reply Last reply
    0
    • P Offline
      P Offline
      patrickkidd
      wrote on last edited by patrickkidd
      #7

      I figured it out. I was setting the Qt property to constant in the declaration. Apparently Qml caches the property somewhere in the Qml internals and never queries for it again.

      I had misunderstood the constant property attribute to mean that it was read-only, i.e. there was no setter. In fact, constant means it never changes over the course of the application's life cycle.

      So that is one additional condition where which the Text.text would not update when the property notify signals are emitted.

      https://alaskafamilysystems.com/

      1 Reply Last reply
      1
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by fcarney
        #2

        It looks to me that the signal from marriageModel is not the same as marriageModel.personAName. Is there a signal associated with personAName and personBName?

        Edit:
        I just saw that there are signals. It should be updating then. What does your property code look like?

        C++ is a perfectly valid school of magic.

        P 1 Reply Last reply
        0
        • fcarneyF fcarney

          It looks to me that the signal from marriageModel is not the same as marriageModel.personAName. Is there a signal associated with personAName and personBName?

          Edit:
          I just saw that there are signals. It should be updating then. What does your property code look like?

          P Offline
          P Offline
          patrickkidd
          wrote on last edited by
          #3

          @fcarney said in Binding does not update property but Connections object does:

          It looks to me that the signal from marriageModel is not the same as marriageModel.personAName. Is there a signal associated with personAName and personBName?

          Edit:
          I just saw that there are signals. It should be updating then. What does your property code look like?

          Could you please clarify? The second example shows the notify signal names associated with the properties.

          https://alaskafamilysystems.com/

          1 Reply Last reply
          0
          • GrecKoG Online
            GrecKoG Online
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #4

            What is the type of marriageModel and is personAName a property of it?

            Can you share the .h where you declared your properties?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              patrickkidd
              wrote on last edited by
              #5

              Marriage model is a QObject and the two signals we’re added as the notify signals for the two properties. Qml automatically derived the onPersonANameChanged property name from the standard naming scheme.

              These properties are automatically generated in python and I unfortunately can’t share the code as it’s too complicated. So I am asking more from a carefully theoretical perspective about the Qml binding mechanism. So far as I can tell, the example should be self explanatory.

              https://alaskafamilysystems.com/

              1 Reply Last reply
              0
              • GrecKoG Online
                GrecKoG Online
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #6

                text: "%1, %2".arg(marriageModel.personAName).arg(marriageModel.personBName)

                This binding should update itself if the personAName or personBName notify signal is emitted.
                Unless you share a minimal reproducible example we can't really help you more.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  patrickkidd
                  wrote on last edited by patrickkidd
                  #7

                  I figured it out. I was setting the Qt property to constant in the declaration. Apparently Qml caches the property somewhere in the Qml internals and never queries for it again.

                  I had misunderstood the constant property attribute to mean that it was read-only, i.e. there was no setter. In fact, constant means it never changes over the course of the application's life cycle.

                  So that is one additional condition where which the Text.text would not update when the property notify signals are emitted.

                  https://alaskafamilysystems.com/

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved