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

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(clean-up)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:snippets]]
[[Category:snippets]]
= 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
main.cpp
<code>
<code>
#include <QtGui>
#include <QtGui>
Line 16: Line 14:
  Q_OBJECT
  Q_OBJECT
public:
public:
 
Q_INVOKABLE void triggerEvent(QString text) {
Q_INVOKABLE void triggerEvent(QString text)
  emit somethingHappened(text);
{
emit somethingHappened(text);
  }
  }
  Person()
  Person() {
{
  startTimer(1000);
startTimer(1000);
  }
  }
  void timerEvent(QTimerEvent '''e)
  void timerEvent(QTimerEvent *e) {
{
  QString text = "The text";
QString text = "The text";
  triggerEvent(text);
triggerEvent(text);
  }
  }
signals:
signals:
Line 35: Line 29:


#include "main.moc"
#include "main.moc"
int main(int argc, char'''* argv)
 
int main(int argc, char** argv)
{
{
  QApplication app(argc, argv);
  QApplication app(argc, argv);
Line 46: Line 41:
}
}
</code>
</code>
main.qml
main.qml
<code>
<code>
import People 1.0
import People 1.0
Line 54: Line 51:
  id: myPerson
  id: myPerson
  onSomethingHappened: {
  onSomethingHappened: {
console.log(" something happened " +text);
  console.log(" something happened " +text);
  }
  }
}
}
</code>

Revision as of 14:01, 24 March 2016

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);
 }
}