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

Binding an attached property instance to a Connection.target results in attachee being set as a target

    XMLWordPrintable

Details

    • All
    • 9f089022bec116b1a7ae415c431768d5b6a67a8a (qt/qtdeclarative/6.2)

    Description

      Binding to an attached object's signal from outside is virtually impossible. It results in many weird errors on multiple levels.

      See attached (whoosh) project for attempts to handle Keys.onReturnPressed signal of a text field, given that text field as a variable/property value.

      Here are chunks of that code:

      TextField {
          id: tf
      }
      
      // error: qrc:/main.qml:24:5: QML Connections: Detected function "onReturnPressed" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.
      Connections {
          target: tf.Keys
          function onReturnPressed() {
              tf.text = "<Return>";
          }
      }
      
      // error: qrc:/main.qml:34:5: QML Binding: Property 'Keys.onReturnPressed' on TextField is read-only.
      Binding {
          target: tf
          property: 'Keys.onReturnPressed'
          value: (event) => {
              tf.text = "<Return>";
          }
      }
      
      Connections {
          id: con2
          target: tf
      
          // there's no syntax for arbitrary function names in JavaScript
          //function `onKeys.ReturnPressed`() {
          //    tf.text = "<Return>";
          //}
      
          // `Symbols as function names` aren't supported in QML in such form either.
          // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#symbols_as_function_names
          //[Symbol("Keys.ReturnPressed")]: {
          //    tf.text = "<Return>";
          //}
      
          Component.onCompleted: {
              // error: qrc:/main.qml:41: TypeError: Value is undefined and could not be converted to an object
              con2.__prototype__[Symbol("Keys.ReturnPressed")] = () => {
                  tf.text = "<Return>";
              };
          }
      }
      

      Note that it is possible to assign and pass around attached object by itself, so this is definitely not a general issue of type system; only Connections QML Type seem to be flawed. In the following case we can observe one object being printed twice to a console:

      property var keyz: tf.Keys
      onKeyzChanged: print("keyz =", keyz, ", tf.Keys =", tf.Keys);
      // qml: keyz = QQuickKeysAttached(0x561fbedff360) , tf.Keys = QQuickKeysAttached(0x561fbedff360)
      

      Note that documentation says nothing about this topic, at all.

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-97427
          # Subject Branch Project Status CR V

          Activity

            People

              qtqmlteam Qt Qml Team User
              ratijas ivan tkachenko
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes