How to do it…

Let's go through the following steps:

  1. Once again, we will start everything from scratch. Therefore, create a new Qt Quick application – Empty project in Qt Creator and create MainForm.ui.qml. Then, open up MainForm.ui.qml.
  2. We can keep the mouse area and text widget, but place the text widget at the bottom of the window. Change the Text property of the text widget to Change this text using C++ and set its font size to 18. After that, go to the Layout tab and enable both Vertical center anchor and Horizontal center anchor to ensure it's always somewhere in the middle of the window, regardless of how you rescale the window.

Set the Margin for the Vertical center anchor to 120 as shown in the following screenshot:

  1. Next, drag a rectangle widget from the Library window to the canvas and set its color to #ff0d0d. Set its Width and Height to 200 and enable both the vertical and horizontal center anchor. After that, set the Margin of the horizontal center anchor to -14. Your UI should now look something like this:

  1. Once you are done with that, right-click on your project directory in Qt Creator and choose Add New..., as shown in the following screenshot. Then, a window will pop up and let you pick a file template. Select C++ Class and press Choose…. After that, it will ask you to define the C++ class by filling in the information for the class. In this case, insert MyClass in the Class Name field and select QObject as the Base class. Then, make sure the Include QObject option is ticked, and you can now click the Next button, followed by the Finish button. Two files—myclass.h and myclass.cpp, will now be created and added to your project:

  1. Now, open up myclass.h and add a variable and function under the class constructor, as shown in the following code:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
// Object pointer
QObject* myObject;
// Must call Q_INVOKABLE so that this function can be used in QML
Q_INVOKABLE void setMyObject(QObject* obj);
};
#endif // MYCLASS_H
  1. After that, open up myclass.cpp and define the setMyObject() function as follows:
#include "myclass.h"
MyClass::MyClass(QObject *parent) : QObject(parent)
{
}
void MyClass::setMyObject(QObject* obj)
{
// Set the object pointer
myObject = obj;
}
  1. We can now close myclass.cpp and open up main.qml. At the top of the file, add in the third line, which imports the custom library we just created in C++:
import QtQuick 2.9
import QtQuick.Window 2.3
import MyClassLib 1.0
  1. Then, define MyClass in the Window object and call its setMyObject() function within the MainForm object, as shown in the following code:
Window {
visible: true
width: 480
height: 320
MyClass {
id: myclass
}
MainForm {
anchors.fill: parent
mouseArea.onClicked: {
Qt.quit();
}
Component.onCompleted:
myclass.setMyObject(messageText);
}
}
  1. Lastly, open up main.cpp and register the custom class to the QML engine. We will also change the properties of the text widget and the rectangle here using C++ code as follows:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QQuickView>
#include <QQuickItem>
#include <QQuickView>
#include "myclass.h"
int main(int argc, char *argv[])
{
// Register your class to QML
qmlRegisterType<MyClass>("MyClassLib", 1, 0, "MyClass");
  1. Then, proceed to create the objects, just like the highlighted section in the following code:
    QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QObject* root = engine.rootObjects().value(0);
QObject* messageText = root->findChild<QObject*>("messageText");
messageText->setProperty("text", QVariant("C++ is now in control!"));
messageText->setProperty("color", QVariant("green"));
QObject* square = root->findChild<QObject*>("square");
square->setProperty("color", QVariant("blue"));

return app.exec();
}
  1. Now build and run the program, and you should see the colors of the rectangle and the text are completely different from what you defined earlier in Qt Quick, as shown in the following screenshot. This is because their properties have been changed by the C++ code:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset