Re: QML and C++ interaction
Ah, forgot to mark the function I wish to run in C++ with Q_INVOKABLE. Now works.
Re: QML and C++ interaction
Hi,
I'm having the same issue, can you post how you got it working? the C++ function you just have to surround with Q_+INVOKABLE?
WHat if you want to send a SIGNAL from QML that has data the user entered (from QML). to a C++ slot that will process that data.
Re: QML and C++ interaction
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:
Code:
#include <QObject>
{
Q_OBJECT
public:
explicit MyClass
(QObject *parent
= 0);
Q_INVOKABLE
void doThisAndThat
(QString myString
);
Q_INVOKABLE
QString getThisAndThat
();
signals:
public slots:
private:
};
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:
Code:
QDeclarativeView::rootContext()->setContextProperty("qmlReferenceName", myClass);
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:
Code:
Rectangle {
MouseArea {
anchors.fill: parent
onClicked: { qmlReferenceName.doThisAndThat("It works!") }
}
Text {
text: qmlReferenceName.getThisAndThat()
}
}