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
- relates to
-
QTBUG-114086 [Reg 6.4 -> 6.5] Historical NativeMethodBehavior is broken when a Q_INVOKABLE function is stored in a QML property
-
- Closed
-