Receiving signals with arguments in QML from C++: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
= Receiving signals with arguments in QML from C++ =
= 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.
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<br /><code><br />#include &lt;QtGui&amp;gt;<br />#include &lt;QtDeclarative&amp;gt;
main.cpp<br /><code><br />#include <QtGui><br />#include <QtDeclarative>


class Person : public QObject<br />{<br /> Q_OBJECT<br />public:
class Person : public QObject<br />{<br /> Q_OBJECT<br />public:


Q_INVOKABLE void triggerEvent(QString text)<br /> {<br /> emit somethingHappened(text);<br /> }<br /> Person()<br /> {<br /> startTimer(1000);<br /> }<br /> void timerEvent(QTimerEvent '''e)<br /> {<br /> QString text = &quot;The text&amp;quot;;<br /> triggerEvent(text);<br /> }<br />signals:<br /> void somethingHappened(QString text);<br />};
Q_INVOKABLE void triggerEvent(QString text)<br /> {<br /> emit somethingHappened(text);<br /> }<br /> Person()<br /> {<br /> startTimer(1000);<br /> }<br /> void timerEvent(QTimerEvent '''e)<br /> {<br /> QString text = "The text";<br /> triggerEvent(text);<br /> }<br />signals:<br /> void somethingHappened(QString text);<br />};
<br />#include &quot;main.moc&amp;quot;<br />int main(int argc, char'''* argv)<br />{<br /> QApplication app(argc, argv);<br /> qmlRegisterType&amp;lt;Person&amp;gt;(&quot;People&amp;quot;, 1, 0, &quot;Person&amp;quot;);<br /> QDeclarativeView view;<br /> view.setSource(QUrl::fromLocalFile&amp;amp;#40;&quot;main.qml&amp;quot;&amp;#41;);<br /> view.resize(200,100);<br /> view.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code><br />main.qml<br /><code><br />import People 1.0<br />import QtQuick 1.0
<br />#include "main.moc"<br />int main(int argc, char'''* argv)<br />{<br /> QApplication app(argc, argv);<br /> qmlRegisterType<Person>("People", 1, 0, "Person");<br /> QDeclarativeView view;<br /> view.setSource(QUrl::fromLocalFile("main.qml"));<br /> view.resize(200,100);<br /> view.show();<br /> return app.exec();<br />}<br /></code><br />main.qml<br /><code><br />import People 1.0<br />import QtQuick 1.0


Person {<br /> id: myPerson<br /> onSomethingHappened: {<br /> console.log(&quot; something happened &quot; +text);<br /> }<br />}
Person {<br /> id: myPerson<br /> onSomethingHappened: {<br /> console.log(" something happened " +text);<br /> }<br />}

Revision as of 14:51, 24 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

<br />#include <QtGui><br />#include <QtDeclarative>

class Person : public QObject<br />{<br /> Q_OBJECT<br />public:

Q_INVOKABLE void triggerEvent(QString text)<br /> {<br /> emit somethingHappened(text);<br /> }<br /> Person()<br /> {<br /> startTimer(1000);<br /> }<br /> void timerEvent(QTimerEvent '''e)<br /> {<br /> QString text = "The text";<br /> triggerEvent(text);<br /> }<br />signals:<br /> void somethingHappened(QString text);<br />};
<br />#include "main.moc"<br />int main(int argc, char'''* argv)<br />{<br /> QApplication app(argc, argv);<br /> qmlRegisterType<Person>("People", 1, 0, "Person");<br /> QDeclarativeView view;<br /> view.setSource(QUrl::fromLocalFile("main.qml"));<br /> view.resize(200,100);<br /> view.show();<br /> return app.exec();<br />}<br />


main.qml

import People 1.0
import QtQuick 1.0

Person {
id: myPerson
onSomethingHappened: {
console.log(" something happened " +text);
}
}