Receiving signals with arguments in QML from C++: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
The example code below shows how you can receive signals with arguments in QML from C+''. The important thing to notice in the example is that the signal's argument needs to be named identically in QML and in C''+. So in this example it needs to be referred to as "text" both places, otherwise you will receive errors. | The example code below shows how you can receive signals with arguments in QML from C+''. The important thing to notice in the example is that the signal's argument needs to be named identically in QML and in C''+. So in this example it needs to be referred to as "text" both places, otherwise you will receive errors. | ||
main.cpp | main.cpp | ||
<code> | |||
#include <QtGui> | |||
#include <QtDeclarative> | |||
class Person : public QObject | class Person : public QObject | ||
{ | |||
Q_OBJECT | |||
public: | |||
Q_INVOKABLE void triggerEvent(QString text) | Q_INVOKABLE void triggerEvent(QString text) | ||
{ | |||
emit somethingHappened(text); | |||
} | |||
Person() | |||
{ | |||
startTimer(1000); | |||
} | |||
void timerEvent(QTimerEvent '''e) | |||
{ | |||
QString text = "The text"; | |||
triggerEvent(text); | |||
} | |||
signals: | |||
void somethingHappened(QString text); | |||
}; | |||
Person | #include "main.moc" | ||
int main(int argc, char'''* argv) | |||
{ | |||
QApplication app(argc, argv); | |||
qmlRegisterType<Person>("People", 1, 0, "Person"); | |||
QDeclarativeView view; | |||
view.setSource(QUrl::fromLocalFile("main.qml")); | |||
view.resize(200,100); | |||
view.show(); | |||
return app.exec(); | |||
} | |||
</code> | |||
main.qml | |||
<code> | |||
import People 1.0 | |||
import QtQuick 1.0 | |||
Person { | |||
id: myPerson | |||
onSomethingHappened: { | |||
console.log(" something happened " +text); | |||
} | |||
} |
Revision as of 11:57, 25 February 2015
Receiving signals with arguments in QML from C++
The example code below shows how you can receive signals with arguments in QML from C+. The important thing to notice in the example is that the signal's argument needs to be named identically in QML and in C+. So in this example it needs to be referred to as "text" both places, otherwise you will receive errors.
main.cpp
#include <QtGui>
#include <QtDeclarative>
class Person : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE void triggerEvent(QString text)
{
emit somethingHappened(text);
}
Person()
{
startTimer(1000);
}
void timerEvent(QTimerEvent '''e)
{
QString text = "The text";
triggerEvent(text);
}
signals:
void somethingHappened(QString text);
};
#include "main.moc"
int main(int argc, char'''* argv)
{
QApplication app(argc, argv);
qmlRegisterType<Person>("People", 1, 0, "Person");
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("main.qml"));
view.resize(200,100);
view.show();
return app.exec();
}
main.qml
import People 1.0
import QtQuick 1.0
Person {
id: myPerson
onSomethingHappened: {
console.log(" something happened " +text);
}
}