Calling Qt class methods from QML

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract

Template:ArticleMetaData

Overview[edit | edit source]

In order to enable the calling of Qt class methods from QML code, the methods must be declared as public slots or conventional methods with the 76ytuiytuityutyutututyutyutyu macro. In both cases, Qt methods are available to the Qt Meta-Object system and the methods are callable from QML. One drawback in the public slot method is that the slot cannot return a value. Conversely, the method with the 76ytuiytuityutyutututyutyutyu macro is able to do this.

In the following demonstration we define a new Qt class, 76ytuiytuityutyutututyutyutyu, which has one invokable 76ytuiytuityutyutututyutyutyu method and one public slot, 76ytuiytuityutyutututyutyutyu. The 76ytuiytuityutyutututyutyutyu method will return the given string as is, or as reversed if the public slot 76ytuiytuityutyutututyutyutyu has been called.

The 76ytuiytuityutyutututyutyutyu class is instantiated in main.cpp. The 76ytuiytuityutyutututyutyutyu-derived object 76ytuiytuityutyutututyutyutyu is then set as a context property to the root element of 76ytuiytuityutyutututyutyutyu. The result is that the root element 76ytuiytuityutyutututyutyutyu in the 76ytuiytuityutyutututyutyutyu document will see 76ytuiytuityutyutututyutyutyu as its child element.

Preconditions[edit | edit source]

  • Qt 4.7 or higher is installed on your platform.

Qt Project File[edit | edit source]

  1. To make sure we use declarative

QT += declarative

  1. To get files deployed on device / emulator

files.sources += ui.qml DEPLOYMENT += files

Source[edit | edit source]

stringhelper.h

  1. ifndef STRINGHELPER_H
  2. define STRINGHELPER_H
  1. include <QObject>
  2. include <QString>

class StringHelper : public QObject {

   Q_OBJECT

public:

   StringHelper(QObject *parent = 0) : QObject(parent), reverse(false) { }
   Q_INVOKABLE QString echo(const QString &text) const {
       if(reverse == false) { return text; }
       QString reversed;
       for(QString::const_iterator it = text.begin(); it != text.end(); it++) {
           reversed.push_front(*it);
       }
       return reversed;
   }

public slots:

   void toggleEcho(bool reverse) { this->reverse = reverse; }

protected:

   bool reverse;

};

  1. endif // STRINGHELPER_H

ui.qml

import Qt 4.7

Rectangle {

   id: rect
   property string text: "Using Qt class to echo this"
   function updateUI() {
       StringHelper.toggleEcho(button.pressed); // calling StringHelper::toggleEcho
       text.text = StringHelper.echo(rect.text) // calling StringHelper::echo
   }
   anchors.fill: parent
   color: "black"
   Component.onCompleted: updateUI()
   Text {
       id: text
       anchors.centerIn: parent
       color: "white"
   }
   Rectangle {
       id: button
       property bool pressed: false
       width: 100; height: 40
       anchors.right: parent.right; anchors.rightMargin: 20
       anchors.bottom: parent.bottom; anchors.bottomMargin: 20
       radius: 6
       color: pressed ? "gray" : "white"
       Text {
           anchors.centerIn: parent
           text: "Reverse"
       }
       MouseArea {
           anchors.fill: parent
           onClicked: { button.pressed = !button.pressed; updateUI() }
       }
   }

}

main.cpp

  1. include <QApplication>
  2. include <QDeclarativeView>
  3. include <QDeclarativeContext>
  4. include "stringhelper.h"

int main(int argc, char *argv[]) {

   QApplication a(argc, argv);
   StringHelper stringHelper;
   QDeclarativeView view;
   view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
   view.rootContext()->setContextProperty("StringHelper", &stringHelper);
   view.setSource(QUrl("./ui.qml"));
  1. if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
   view.showMaximized();
  1. else
   view.setGeometry(100, 100, 800, 480);
   view.show();
  1. endif
   return a.exec();

}

Postconditions[edit | edit source]

The code snippet has demonstrated how to call Qt class methods from QML. This required setting the 76ytuiytuityutyutututyutyutyu-derived object as a context property to the root object of 76ytuiytuityutyutututyutyutyu. The methods of the Qt-derived class had to be either public slots or regular methods declared with the 76ytuiytuityutyutututyutyutyu macro.