Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-116464

QML/JS signal.connect(): Accept a context object like QObject::connect()

XMLWordPrintable

      If we have QT_LOGGING_RULES="qt.qml.object.connect.info=true;" then the following code produces a runtime message:

      qt.qml.object.connect: Could not find receiver of the connection, using sender as receiver. Disconnect explicitly (or delete the sender) to make sure the connection is removed.
      

      QObject::connect() avoids this scenario by allowing the user to specify a "context object": https://doc.qt.io/qt-6/qobject.html#connect-5 It would be nice if the JS connect allowed the same.

      In addition, the contextless version of connect() should then be discouraged by qmllint, like what clazy does: https://github.com/KDE/clazy/blob/master/docs/checks/README-connect-3arg-lambda.md

       

      Code

      import QtQuick
      
      Window {
          id: root
          width: 640
          height: 480
          visible: true
      
          Component {
              id: comp
              QtObject {
                  signal finished()
                  function doStuff() {
                      // ...
                      finished()
                  }
              }
          }
      
          Component.onCompleted: {
              let obj = comp.createObject(root)
              obj.finished.connect(function() {
                  console.log("Stuff done!")
              })
      
              obj.doStuff()
          }
      }
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            skoh-qt Sze Howe Koh
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes