Using Qt 5.4.0 and Qt Creator 3.3.0, create a new project:
- Click "New Project"
- Qt quick app
- Click "Select ..."
- Name the project and choose where to place it.
- Click "Next"
- Select Qt Quick 2.4 from the drop-down menu
- Click "Next"
- Select the desired kit (s)
- Click "Next"
- Click Finish
You should now see the open main.qml file with the following code:
import QtQuick 2.4 import QtQuick.Window 2.2 Window { visible: true MainForm { anchors.fill: parent mouseArea.onClicked: { Qt.quit(); } } }
Make changes to the file so that it looks like this:
import QtQuick 2.4 import QtQuick.Window 2.2 Window { visible: true // signal myQmlSignal(string msg) // New Code
Add a new class to your project:
- Right mouse Click project name in the project view
- Click Add New ...
- Select C ++ class if not already selected
- Click "Select ..."
- In the Class Name field, enter "MyCppClass"
- Set base class in QObject
- Click "Next"
- Click Finish
Open the mycppclass.h file, it should look like this:
#ifndef MYCPPCLASS_H #define MYCPPCLASS_H #include <QObject> class MyCppClass : public QObject { Q_OBJECT public: explicit MyCppClass(QObject *parent = 0); ~MyCppClass(); signals: public slots: }; #endif // MYCPPCLASS_H
Make changes to mycppclass.h so it looks like this:
#ifndef MYCPPCLASS_H #define MYCPPCLASS_H #include <QObject> //### New Code ### #include <QDebug> //################ class MyCppClass : public QObject { Q_OBJECT public: explicit MyCppClass(QObject *parent = 0); ~MyCppClass(); signals: public slots: //### New Code ### void myCppSlot(const QString &msg); //################ }; #endif // MYCPPCLASS_H
Open mycppclass.cpp, which should look like this:
#include "mycppclass.h" MyCppClass::MyCppClass(QObject *parent) : QObject(parent) { } MyCppClass::~MyCppClass() { }
Change it like this:
#include "mycppclass.h" MyCppClass::MyCppClass(QObject *parent) : QObject(parent) { } MyCppClass::~MyCppClass() { } void MyCppClass::myCppSlot(const QString &msg) {
Open main.cpp, which looks like this:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
And make the following changes:
#include <QGuiApplication> #include <QQmlApplicationEngine> //### New Code ### #include "mycppclass.h" //################ int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); //### New Code ### MyCppClass myCppClass; QObject::connect(engine.rootObjects().takeFirst(), SIGNAL(myQmlSignal(QString)), &myCppClass, SLOT(myCppSlot(QString))); //################ return app.exec(); }
Click the big green triangle to compile and run the application. Look at the application output area, click "Hello World", you will see the following message:
qml: sending myQmlSignal from QML ...
The signal was received by C ++. It contains the following message: "Hello from QML"