Binding a QML property to a C++ function

At the moment Qt Quick does not provide an implementation for linking a QML property to the result of a C++ function. There exists a suggestion for adding support for this here [bugreports.qt.nokia.com] though.

In this article we will show how to make binding of properties to C++ functions work using Q_PROPERTY [doc.qt.nokia.com] with a NOTIFY [doc.qt.nokia.com] signal that is emitted whenever the value of the property changes.

The C++ implementation

In the Object class in the example below we create a Q_PROPERTY that is used to set and update the text in the QML code and that has a changeOfStatus() signal which is emitted whenever the status of the C++ function someFunction() changes:

  1. Q_PROPERTY(QString theChange READ getTheChange NOTIFY changeOfStatus)

The Object::getTheChange() function sets the text of the QML item depending on the result of Object::someFunction(). The result is set as the value of the text property in the QML code.

Object::someFunction() is callable from QML as it uses Q_INVOKABLE [doc.qt.nokia.com]. It simply changes the status of a member variable and is called every 5 seconds using a QTimer [doc.qt.nokia.com] to illustrate how the QML property is able to react to the changes of this function. It can also be called from the QML code when clicking the text. At the end it emits the changeOfStatus() signal which triggers Object::getTheChange() to be called and the text to be reevaluated.

main.cpp

  1. #include <QtGui>
  2. #include <QtDeclarative>
  3.  
  4. class Object : public QObject
  5. {
  6.  Q_OBJECT
  7.   Q_PROPERTY(QString theChange READ getTheChange NOTIFY changeOfStatus)
  8.    
  9. public:
  10.  Object()
  11.  {
  12.   changeMe = false;
  13.   myTimer = new QTimer(this);
  14.   myTimer->start(5000);
  15.   connect(myTimer, SIGNAL(timeout()), this, SLOT(testSlot()));
  16.  }
  17.  
  18.  QString getTheChange()
  19.  {
  20.   if (theValue == 0)
  21.   {
  22.    return "The text changed";
  23.   }
  24.   if (theValue == 1)
  25.   {
  26.    return "New text change";
  27.   }
  28.         return "nothing has happened yet";
  29.  
  30.  }
  31.  
  32.  Q_INVOKABLE void someFunction(int i)
  33.  {
  34.   if ( i == 0) {
  35.    theValue = 0;
  36.   }
  37.   if (i == 1) {
  38.    theValue = 1;
  39.   }
  40.  
  41.   emit changeOfStatus(i);
  42.  }
  43.  
  44. signals:
  45.  void changeOfStatus(int i) ;
  46.  public slots:
  47.   void testSlot()
  48.   {
  49.    if (changeMe)
  50.    {
  51.     someFunction(0);
  52.    } else {
  53.     someFunction(1);
  54.    }
  55.    changeMe = !changeMe;
  56.   }
  57.  
  58. private:
  59.  bool changeMe;
  60.  int theValue;
  61.  QTimer *myTimer;
  62.  
  63. };
  64.  
  65. #include "main.moc"
  66.  
  67. int main(int argc, char** argv)
  68. {
  69.  QApplication app(argc, argv);
  70.  Object myObj;
  71.  QDeclarativeView view;
  72.  view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);
  73.  view.setSource(QUrl::fromLocalFile("main.qml"));
  74.  view.show();
  75.  return app.exec();
  76.  
  77. }

The QML implementation

In the QML code below we create a Rectangle [doc.qt.nokia.com] that reacts to mouse clicks. The text is set to the the result of the Object::theChange() function.

main.qml

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     width: 440; height: 150
  5.  
  6.     Column {
  7.         anchors.fill: parent; spacing: 20
  8.  
  9.         Text {
  10.             text: rootItem.theChange
  11.             font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
  12.  
  13.             }
  14.             }
  15. }

So, using the approach in the example above, we get a way for QML properties to react to changes that happen internally in the C++ code.

Categories: