According to Qt5 documentation: exposing methods including qt slots all public slots of C++ classes that inherit from QObject, are accessible from QML Here what I did:
C++
class MyClass : public QObject
{
Q_OBJECT
public slots:
void doStuffFromQmlSlot()
{
qDebug() << Q_FUNC_INFO;
}
public:
MyClass()
{
qDebug() << Q_FUNC_INFO;
}
};
my main.cpp contains:
MyClass myClass;
QQmlEngine engine;
engine.rootContext()->setContextProperty( "myclass", &myClass );
QQmlComponent component( &engine, QUrl::fromLocalFile("qml/qtquick-01/main.qml") );
component.create();
QML
import QtQuick 2.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
myclass.doStuffFromQmlSlot();
Qt.quit();
}
}
}
Actually the QtCreator seems to recognixe the exposed myclass object into the QML as it enables auto-completion of both class name (myclass) and the public slot doStuffFromQmlSlot(). When I run the application I unfortunately got the following error:
ReferenceError: myclass is not defined
Any clue on what I'm doing wrong?