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. Property assignments from JavaScript signal handling
Forum Updated to NodeBB v4.3 + New Features

Property assignments from JavaScript signal handling

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 484 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.
  • B Online
    B Online
    Bob64
    wrote on last edited by
    #1

    I have a situation where I have a group of buttons acting as a kind of toolbar for a component. There are some slightly tricky dependencies between the buttons (eg one button might activate a mode in which other buttons are enabled or disabled) as well dependencies in the component itself on what has been selected via the buttons.

    All these dependencies are expressed in terms of declarative property bindings in the usual QML manner.

    What I am feeling uneasy about is that this is all being driven by property assignments in Button.onClicked handlers. My understanding, which is probably incomplete, is that assignments in JS can lead to property bindings being broken, though in fact I haven't seen any issues yet.

    Can anyone clarify the rules about what can and can't be relied upon? If I should avoid doing this, what would be the approach recommended instead?

    Gojir4G 1 Reply Last reply
    0
    • B Bob64

      I have a situation where I have a group of buttons acting as a kind of toolbar for a component. There are some slightly tricky dependencies between the buttons (eg one button might activate a mode in which other buttons are enabled or disabled) as well dependencies in the component itself on what has been selected via the buttons.

      All these dependencies are expressed in terms of declarative property bindings in the usual QML manner.

      What I am feeling uneasy about is that this is all being driven by property assignments in Button.onClicked handlers. My understanding, which is probably incomplete, is that assignments in JS can lead to property bindings being broken, though in fact I haven't seen any issues yet.

      Can anyone clarify the rules about what can and can't be relied upon? If I should avoid doing this, what would be the approach recommended instead?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      Hi @Bob64,

      You need to use Qt.binding() for that:

      here is an example from Creating Property Bindings from JavaScript

      import QtQuick 2.0
      
      Rectangle {
          width: 100
          height: width * 2
      
          focus: true
          Keys.onSpacePressed: {
              height = Qt.binding(function() { return width * 3 })
          }
      }
      
      B 1 Reply Last reply
      1
      • Gojir4G Gojir4

        Hi @Bob64,

        You need to use Qt.binding() for that:

        here is an example from Creating Property Bindings from JavaScript

        import QtQuick 2.0
        
        Rectangle {
            width: 100
            height: width * 2
        
            focus: true
            Keys.onSpacePressed: {
                height = Qt.binding(function() { return width * 3 })
            }
        }
        
        B Online
        B Online
        Bob64
        wrote on last edited by
        #3

        @Gojir4 Thanks. I think this has helped to clarify the issue for me. In my case I am simply assigning boolean values to properties in my onClicked handlers. These properties never had a binding to other properties so the assignment of the new value does not break any binding. Now, there are property bindings elsewhere that are dependent on the properties that I am assigning but these changes are propagated OK as far as I can see, and I now think that this is what we would expect.

        Gojir4G 1 Reply Last reply
        0
        • B Bob64

          @Gojir4 Thanks. I think this has helped to clarify the issue for me. In my case I am simply assigning boolean values to properties in my onClicked handlers. These properties never had a binding to other properties so the assignment of the new value does not break any binding. Now, there are property bindings elsewhere that are dependent on the properties that I am assigning but these changes are propagated OK as far as I can see, and I now think that this is what we would expect.

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @Bob64 Glad it helps.

          Please don't forget to set the post as solved if this answer to your question
          Thanks!

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

            My rule of thumb is to act on data, not directly UI objects.

            In your case it would mean that instead of doing something like this:

            Row {
                Button {
                    text: "Change mode"
                    onClicked: {
                        fooButton.enabled  = !fooButton.enabled
                        barButton.enabled = !barButton.enabled
                    }
                }
                Button {
                    id: fooButton
                    text: "Foo"
                    enabled: true
                    onClicked: doFoo()
                }
                Button {
                    id: barButton
                    text: "Bar"
                    enabled: false
                    onClicked: doBar()
                }
            }
            

            You'll do the following:

            Row {
                id: buttonRow
                property bool myMode: true
                Button {
                    text: "Change mode"
                    onClicked: buttonRow.myMode = !buttonRow.myMode
                }
                Button {
                    text: "Foo"
                    enabled: buttonRow.myMode
                    onClicked: doFoo()
                }
                Button {
                    text: "Bar"
                    enabled: !buttonRow.myMode
                    onClicked: doBar()
                }
            }
            

            Does that help?

            B 1 Reply Last reply
            3
            • GrecKoG GrecKo

              My rule of thumb is to act on data, not directly UI objects.

              In your case it would mean that instead of doing something like this:

              Row {
                  Button {
                      text: "Change mode"
                      onClicked: {
                          fooButton.enabled  = !fooButton.enabled
                          barButton.enabled = !barButton.enabled
                      }
                  }
                  Button {
                      id: fooButton
                      text: "Foo"
                      enabled: true
                      onClicked: doFoo()
                  }
                  Button {
                      id: barButton
                      text: "Bar"
                      enabled: false
                      onClicked: doBar()
                  }
              }
              

              You'll do the following:

              Row {
                  id: buttonRow
                  property bool myMode: true
                  Button {
                      text: "Change mode"
                      onClicked: buttonRow.myMode = !buttonRow.myMode
                  }
                  Button {
                      text: "Foo"
                      enabled: buttonRow.myMode
                      onClicked: doFoo()
                  }
                  Button {
                      text: "Bar"
                      enabled: !buttonRow.myMode
                      onClicked: doBar()
                  }
              }
              

              Does that help?

              B Online
              B Online
              Bob64
              wrote on last edited by
              #6

              @GrecKo Thank you. That is a useful point to make and is in fact very close to the sort of thing I am doing.

              I think my problem had been that I had missed the point about what the potential issue actually is in JS property assignments. I had it in my head that it was something to avoid when there are bindings dependent on the property being set, but really it's only an issue if existing bindings are being broken by the assignment. I hadn't been forced to think about it properly because all of the bindings I had relied upon so far either depended on properties provided by QML itself or on properties I am providing from my C++ backend objects. And in the places I was already setting properties directly in JS, I was not worried about them because they were typically for 'one-shot' uses - e.g. setting something on a dialog or a popup menu just before showing it.

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @Bob64 I see that you've received the clarifications you need for this particular issue.

                If you'd like to further your understanding, see this article: https://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                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