TypeError: Property '' of object QObject(0x6cfdf0) is not a function
-
Hi,
I'm following an example on how to connect qml and c++. ( this is the link of the example chapter Q_INVOKABLE).
My program is the following:main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "receiver.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; Receiver rcvr; engine.rootContext()->setContextProperty("Receiver", &rcvr); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
Receiver.h
#include <QObject> #include <QDebug> class Receiver : public QObject { public: Receiver(); Q_INVOKABLE bool postMessage(const QString &msg) { qDebug() << "Called the C++ method with" << msg; return true; } };
main.qml
import QtQuick 2.9 import QtQuick.Controls 2.2 ApplicationWindow { id: window visible: true width: 640 height: 480 title: qsTr("Stack") header: ToolBar { contentHeight: toolButton.implicitHeight ToolButton { id: toolButton text: /*stackView.depth > 1 ? "\u25C0" :*/ "\u2630" font.pixelSize: Qt.application.font.pixelSize * 1.6 onClicked: { // if (stackView.depth > 1) { // stackView.pop() // } else { drawer.open() var result = Receiver.postMessage("Hello from QML") console.log("Result of postMessage():", result) // } } } Label { text: stackView.currentItem.title anchors.centerIn: parent } } Drawer { id: drawer width: window.width * 0.66 height: window.height Column { anchors.fill: parent ItemDelegate { text: qsTr("Page 1") width: parent.width onClicked: { stackView.push("Page1Form.ui.qml") drawer.close() } } ItemDelegate { text: qsTr("Page 2") width: parent.width onClicked: { stackView.push("Page2Form.ui.qml") drawer.close() } } } } StackView { id: stackView initialItem:"Page1Form.ui.qml"//"HomeForm.ui.qml" anchors.fill: parent } }
Now when I click on the toolbar I try to call the function on Receiver.h I get this error:
"qrc:/main.qml:23: TypeError: Property 'postMessage' of object QObject(0x6cfdf0) is not a function"Anybody knows what I'm doing wrong here? Thanks in advance!
-
Hi,
I'm following an example on how to connect qml and c++. ( this is the link of the example chapter Q_INVOKABLE).
My program is the following:main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "receiver.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; Receiver rcvr; engine.rootContext()->setContextProperty("Receiver", &rcvr); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
Receiver.h
#include <QObject> #include <QDebug> class Receiver : public QObject { public: Receiver(); Q_INVOKABLE bool postMessage(const QString &msg) { qDebug() << "Called the C++ method with" << msg; return true; } };
main.qml
import QtQuick 2.9 import QtQuick.Controls 2.2 ApplicationWindow { id: window visible: true width: 640 height: 480 title: qsTr("Stack") header: ToolBar { contentHeight: toolButton.implicitHeight ToolButton { id: toolButton text: /*stackView.depth > 1 ? "\u25C0" :*/ "\u2630" font.pixelSize: Qt.application.font.pixelSize * 1.6 onClicked: { // if (stackView.depth > 1) { // stackView.pop() // } else { drawer.open() var result = Receiver.postMessage("Hello from QML") console.log("Result of postMessage():", result) // } } } Label { text: stackView.currentItem.title anchors.centerIn: parent } } Drawer { id: drawer width: window.width * 0.66 height: window.height Column { anchors.fill: parent ItemDelegate { text: qsTr("Page 1") width: parent.width onClicked: { stackView.push("Page1Form.ui.qml") drawer.close() } } ItemDelegate { text: qsTr("Page 2") width: parent.width onClicked: { stackView.push("Page2Form.ui.qml") drawer.close() } } } } StackView { id: stackView initialItem:"Page1Form.ui.qml"//"HomeForm.ui.qml" anchors.fill: parent } }
Now when I click on the toolbar I try to call the function on Receiver.h I get this error:
"qrc:/main.qml:23: TypeError: Property 'postMessage' of object QObject(0x6cfdf0) is not a function"Anybody knows what I'm doing wrong here? Thanks in advance!
@davidesalvetti hi
your class needs Q_OBJECT macro -
Hi,
I'm following an example on how to connect qml and c++. ( this is the link of the example chapter Q_INVOKABLE).
My program is the following:main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "receiver.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; Receiver rcvr; engine.rootContext()->setContextProperty("Receiver", &rcvr); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
Receiver.h
#include <QObject> #include <QDebug> class Receiver : public QObject { public: Receiver(); Q_INVOKABLE bool postMessage(const QString &msg) { qDebug() << "Called the C++ method with" << msg; return true; } };
main.qml
import QtQuick 2.9 import QtQuick.Controls 2.2 ApplicationWindow { id: window visible: true width: 640 height: 480 title: qsTr("Stack") header: ToolBar { contentHeight: toolButton.implicitHeight ToolButton { id: toolButton text: /*stackView.depth > 1 ? "\u25C0" :*/ "\u2630" font.pixelSize: Qt.application.font.pixelSize * 1.6 onClicked: { // if (stackView.depth > 1) { // stackView.pop() // } else { drawer.open() var result = Receiver.postMessage("Hello from QML") console.log("Result of postMessage():", result) // } } } Label { text: stackView.currentItem.title anchors.centerIn: parent } } Drawer { id: drawer width: window.width * 0.66 height: window.height Column { anchors.fill: parent ItemDelegate { text: qsTr("Page 1") width: parent.width onClicked: { stackView.push("Page1Form.ui.qml") drawer.close() } } ItemDelegate { text: qsTr("Page 2") width: parent.width onClicked: { stackView.push("Page2Form.ui.qml") drawer.close() } } } } StackView { id: stackView initialItem:"Page1Form.ui.qml"//"HomeForm.ui.qml" anchors.fill: parent } }
Now when I click on the toolbar I try to call the function on Receiver.h I get this error:
"qrc:/main.qml:23: TypeError: Property 'postMessage' of object QObject(0x6cfdf0) is not a function"Anybody knows what I'm doing wrong here? Thanks in advance!
@davidesalvetti in addition to what @LeLev said, your constructor is incomplete, you have to properly initialize the base class (QObject in this case)
-
@davidesalvetti in addition to what @LeLev said, your constructor is incomplete, you have to properly initialize the base class (QObject in this case)
-
@J.Hilk eh, not really. It's just that there won't be any QObject parent passed to the QObject constructor.