Haven't yet needed a signal slot mechanism between QML and Qt but here's how you can call C++ code from QML:
First you need your class:
#include <QObject>
{
Q_OBJECT
public:
explicit MyClass
(QObject *parent
= 0);
Q_INVOKABLE
void doThisAndThat
(QString myString
);
Q_INVOKABLE
QString getThisAndThat
();
signals:
public slots:
private:
};
#include <QObject>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
Q_INVOKABLE void doThisAndThat(QString myString);
Q_INVOKABLE QString getThisAndThat();
signals:
public slots:
private:
QString localThisAndThat;
};
To copy to clipboard, switch view to plain text mode
And you can create the implementation as you wish, no need to use the Q_INVOKABLE on the implementation side, though.
The you need to introduce this class to QML like this:
QDeclarativeView::rootContext()->setContextProperty("qmlReferenceName", myClass);
QDeclarativeView::rootContext()->setContextProperty("qmlReferenceName", myClass);
To copy to clipboard, switch view to plain text mode
So you just give a string of your choice to qmlReferenceName, and pass your class on myClass. After this, you have this class as a global scope variable within your QML code:
Rectangle {
MouseArea {
anchors.fill: parent
onClicked: { qmlReferenceName.doThisAndThat("It works!") }
}
Text {
text: qmlReferenceName.getThisAndThat()
}
}
Rectangle {
MouseArea {
anchors.fill: parent
onClicked: { qmlReferenceName.doThisAndThat("It works!") }
}
Text {
text: qmlReferenceName.getThisAndThat()
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks