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

Need "NativeMethodBehavior: AcceptThisObject" (or a separate pragma) to apply to JS functions

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 6.5.1
    • None
    • Windows 10 22H2, MSVC 2019 x64

    Description

      Code
      cppobj.h

      class CppObj : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
      
      public:
          CppObj(QObject* parent = nullptr) : QObject(parent) {}
      
          Q_INVOKABLE void greet()
          {
              qDebug().noquote() << this << "says hello";
          }
      };
      

       

      Main.qml

      //pragma NativeMethodBehavior: AcceptThisObject
      
      import QtQuick.Controls.Basic
      import QmlOtherThis
      
      ApplicationWindow {
          width: 640
          height: 480
          visible: true
      
          property var qmlMethod: function() { console.log(`${this} says hello`) }
          property var cppMethod: objA.greet
      
          CppObj { id: objA; objectName: "objA" }
          CppObj { id: objB; objectName: "objB" }
      
          Button {
              text: "Calculate"
              onClicked: {
                  let jsMethod = ()=>{console.log(`${this} says hello`)}
      
                  jsMethod.call(objB)
                  objA.greet.call(objB)
                  cppMethod.call(objB)
                  qmlMethod.call(objB)
              }
          }
      }
      

       

      Output WITHOUT pragma NativeMethodBehavior: AcceptThisObject

      qml: Button_QMLTYPE_2(0x214966beab0) says hello
      CppObj(0x214966c40c0, name = objA) says hello
      CppObj(0x214966c40c0, name = objA) says hello
      qml: CppObj(0x214966c42e0, "objB") says hello
      

      Note: This is only for the 1st click. The 2nd and subsequent clicks are affected by QTBUG-114086

       

      Output WITH pragma NativeMethodBehavior: AcceptThisObject

      qml: Button_QMLTYPE_2(0x165ef85e770) says hello
      CppObj(0x165ecd8e590, name = objB) says hello
      CppObj(0x165ecd8e590, name = objB) says hello
      qml: CppObj(0x165ecd8e590, "objB") says hello
      

       

      Bug
      pragma NativeMethodBehavior: AcceptThisObject ensures that most calls have objB as 'this', jsMethod.call(objB) still has the Button as 'this'

      Attachments

        Issue Links

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

          Activity

            People

              qtqmlteam Qt Qml Team User
              skoh-qt Sze Howe Koh
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes