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. [SOLVED] Connections with null target
QtWS25 Last Chance

[SOLVED] Connections with null target

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 3.0k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hi,

    I encountered some unexpected behaviour while dealing with the Connections object and its target property. The documentation says:

    If set to null, no connection is made and any signal handlers are ignored until the target is not null.

    Now have a look at the following snippet:

    Item { 
        property var databaseTable: null
        Connections {
            target: databaseTable
            onRowAppended: model.append(row)
        }
    }
    

    This leads to the following runtime error:

    QML Connections: Cannot assign to non-existent property "onRowAppended"

    But the following produces no error:

    Item { 
        property var databaseTable: null
        Connections {
            target: databaseTable==null ? null : databaseTable
            onRowAppended: model.append(row)
        }
    }
    

    Can anyone please give an explanation for this?

    P.S.: I know there is ignoreUnknownSignals: bool, but that's not the point here.

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

      Interesting! Sounds like a bug, as I'd expect both to behave the same.

      You can report this to https://bugreports.qt.io/

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

      ? 1 Reply Last reply
      0
      • JKSHJ JKSH

        Interesting! Sounds like a bug, as I'd expect both to behave the same.

        You can report this to https://bugreports.qt.io/

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @JKSH Yes, looks like a bug to me, too, but maybe it's just another Javascript craziness?

        JKSHJ 1 Reply Last reply
        0
        • ? A Former User

          @JKSH Yes, looks like a bug to me, too, but maybe it's just another Javascript craziness?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Wieland: I'm not sure. See if anyone with more experience replies. If not, you could ask on the Interest mailing list where the Qt engineers hang out (you need to subscribe first)

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

          ? 1 Reply Last reply
          0
          • JKSHJ JKSH

            @Wieland: I'm not sure. See if anyone with more experience replies. If not, you could ask on the Interest mailing list where the Qt engineers hang out (you need to subscribe first)

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @JKSH Thanks for your help! I just figured out what's going wrong here. It's not a bug, but all my fault:

            Item { 
                property var databaseTable: null
                Connections {
                    target: databaseTable     // <- tries to access the *global* object "databaseTable"
                    onRowAppended: model.append(row)
                }
            }
            

            Change it to:

            Item { 
                id: myItem
                property var databaseTable: null
                Connections {
                    target: myItem.databaseTable   
                    onRowAppended: model.append(row)
                }
            }
            

            ... and it accesses the intended local object.

            So, the $500 question: Why the heck does target: databaseTable==null ? null : databaseTable not cause an error?

            Here is the answer: The situation in my actual code looks like this:

            Item {
                id: someItemUpInTheHierarchy
                property var databaseTable      // not initalized to *null*, so it is *undefined*
                // ... lots of code ...
                Item { 
                    property var databaseTable: null
                    Connections {
                        target: databaseTable==null ? null : databaseTable  // undefined==null evaluates to *true*
                        onRowAppended: model.append(row)
                    }
                }
            }
            

            Stupid me! But Javascript is stupid, too ;-)

            JKSHJ Aleksey_KA 2 Replies Last reply
            1
            • ? A Former User

              @JKSH Thanks for your help! I just figured out what's going wrong here. It's not a bug, but all my fault:

              Item { 
                  property var databaseTable: null
                  Connections {
                      target: databaseTable     // <- tries to access the *global* object "databaseTable"
                      onRowAppended: model.append(row)
                  }
              }
              

              Change it to:

              Item { 
                  id: myItem
                  property var databaseTable: null
                  Connections {
                      target: myItem.databaseTable   
                      onRowAppended: model.append(row)
                  }
              }
              

              ... and it accesses the intended local object.

              So, the $500 question: Why the heck does target: databaseTable==null ? null : databaseTable not cause an error?

              Here is the answer: The situation in my actual code looks like this:

              Item {
                  id: someItemUpInTheHierarchy
                  property var databaseTable      // not initalized to *null*, so it is *undefined*
                  // ... lots of code ...
                  Item { 
                      property var databaseTable: null
                      Connections {
                          target: databaseTable==null ? null : databaseTable  // undefined==null evaluates to *true*
                          onRowAppended: model.append(row)
                      }
                  }
              }
              

              Stupid me! But Javascript is stupid, too ;-)

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @Wieland said:

              Stupid me! But Javascript is stupid, too ;-)

              Stupidities make life more interesting ;-) Glad you've solved it! Thanks for sharing your story and solution.

              P.S. This is one reason why I like to explicitly state the id (id.propertyName, or at least parent.propertyName) if the property doesn't belong to the current object. Another user had a similar predicament recently: https://forum.qt.io/topic/53997/

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

              1 Reply Last reply
              0
              • ? A Former User

                @JKSH Thanks for your help! I just figured out what's going wrong here. It's not a bug, but all my fault:

                Item { 
                    property var databaseTable: null
                    Connections {
                        target: databaseTable     // <- tries to access the *global* object "databaseTable"
                        onRowAppended: model.append(row)
                    }
                }
                

                Change it to:

                Item { 
                    id: myItem
                    property var databaseTable: null
                    Connections {
                        target: myItem.databaseTable   
                        onRowAppended: model.append(row)
                    }
                }
                

                ... and it accesses the intended local object.

                So, the $500 question: Why the heck does target: databaseTable==null ? null : databaseTable not cause an error?

                Here is the answer: The situation in my actual code looks like this:

                Item {
                    id: someItemUpInTheHierarchy
                    property var databaseTable      // not initalized to *null*, so it is *undefined*
                    // ... lots of code ...
                    Item { 
                        property var databaseTable: null
                        Connections {
                            target: databaseTable==null ? null : databaseTable  // undefined==null evaluates to *true*
                            onRowAppended: model.append(row)
                        }
                    }
                }
                

                Stupid me! But Javascript is stupid, too ;-)

                Aleksey_KA Offline
                Aleksey_KA Offline
                Aleksey_K
                wrote on last edited by
                #7

                Thanks, had similar bug with target being undefined:

                "Unable to assign [undefined] to QObject*"
                

                Fixed according to the Qt doc:
                https://doc.qt.io/qt-5/qml-qtqml-connections.html#target-prop

                If set to null, no connection is made and any signal handlers are ignored until the target is not null.

                Connections {
                    // If set to null, no connection is made and
                    // any signal handlers are ignored until the target is not null.
                    target: myItem ? myItem : null
                    onPropertyChanged: {
                        ...
                    }
                }
                
                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