9 QML CPP Integration
9 QML CPP Integration
• Declarative Environment
2 © 2015
Objectives
Demo: qml-cpp-integration/ex-clock
3 © 2015
Declarative Environment
Overview
5 © 2015
Setting up a QtQuick Application
#include <QGuiApplication>
#include <QQmlApplicationEngine>
Demo: qml-cpp-integration/ex-simpleviewer
6 © 2015
Setting up QtQuick
QT += quick
RESOURCES = simpleviewer.qrc
SOURCES = main.cpp
Window {
visible: true
width: 400; height: 300
Demo: qml-cpp-integration/ex-simpleviewer
7 © 2015
Exporting C++ Objects to QML
Exporting C++ Objects to QML
9 © 2015
Exporting C++ Objects to QML
QQmlApplicationEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty(”animalModel", &model);
engine.load(QUrl(QStringLiteral("qrc:/view.qml")));
return app.exec();
}
10 © 2015
Using the Object in QML
ListView {
width: 200; height: 250
model: animalModel
11 © 2015
What Is Exported?
• Properties
• Signals
• Slots
• Methods marked with Q_INVOKABLE
• Enums registered with Q_ENUMS
class IntervalSettings : public QObject
{
Q_OBJECT
Q_PROPERTY(int duration READ duration WRITE setDuration
NOTIFY durationChanged)
Q_ENUMS(Unit)
Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged)
public:
enum Unit { Minutes, Seconds, MilliSeconds };
12 © 2015
Exporting Classes to QML
Overview
14 © 2015
Step 1: Implementing the Class
#include <QObject>
class QTimer;
public:
explicit Timer( QObject* parent = 0 );
private:
QTimer* m_timer;
}
15 © 2015
Implementing the Class
16 © 2015
Step 1: Implementing the Class
#include "timer.h"
#include <QTimer>
{
m_timer->setInterval( 1000 );
m_timer->start();
}
17 © 2015
Step 2: Registering the Class
#include "timer.h"
#include <QGuiApplication>
#include <qqml.h> // for qmlRegisterType
#include <QQmlApplicationEngine>
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
18 © 2015
Reviewing the Registration
19 © 2015
Step 3+4 Importing and Using the Class
Window {
visible: true; width: 500; height: 360
Rectangle { anchors.fill: parent
Timer { id: timer }
}
…
}
Demo: qml-cpp-integration/ex_simple_timer
20 © 2015
Adding Properties
Rectangle {
…
Timer {
id: timer
interval: 3000
}
…
Demo: qml-cpp-integration/ex_timer_properties
21 © 2015
Declaring a Property
22 © 2015
Declaring Getter, Setter and Signal
23 © 2015
Implementing Getter and Setter
24 © 2015
Summary of Items and Properties
25 © 2015
Adding Signals
Demo: qml-cpp-integration/ex_timer_signals
26 © 2015
Declaring a Signal
27 © 2015
Emitting the Signal
28 © 2015
Handling the Signal
• In C++:
• The QTimer::timeout() signal is emitted
• Connection means Timer::timeout() is emitted
• In QML:
• The Timer item's onTimeout handler is called
• Outputs message to stderr
29 © 2015
Adding Methods to Items
30 © 2015
Adding Slots
31 © 2015
Adding Slots
Demo: qml-cpp-integration/ex_timer_slots
32 © 2015
Declaring Slots
33 © 2015
Implementing Slots
34 © 2015
Adding Methods
Demo: qml-cpp-integration/ex-methods
35 © 2015
Declaring a Method
36 © 2015
Implementing a Method
37 © 2015
Summary of Signals, Slots and Methods
• Define signals
• Connect to Qt signals with the onSignal syntax
38 © 2015
Exporting a QPainter based GUI Class
• Implement paint(...)
39 © 2015
Exporting a QPainter based GUI Class cont'd.
#include <QQuickPaintedItem>
public:
EllipseItem(QQuickItem *parent = 0);
void paint(QPainter *painter);
};
40 © 2015
Exporting a QPainter based GUI Class cont'd.
EllipseItem::EllipseItem(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
}
painter->drawEllipse(rect);
}
41 © 2015
Exporting a QPainter based GUI Class cont'd.
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "ellipseitem.h"
qmlRegisterType<EllipseItem>("Shapes", 1, 0, "Ellipse");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/ellipse1.qml")));
return app.exec();
}
42 © 2015
Exporting a QPainter based GUI Class cont'd.
Window {
visible: true
width: 300; height: 200
Item {
anchors.fill: parent
Ellipse {
x: 50; y: 50
width: 200; height: 100
}
}
}
Demo: qml-cpp-integration/ex-simple-item
43 © 2015
Exporting a Scene Graph based GUI Class
• Implement updatePaintNode(...)
44 © 2015
Exporting a Scene Graph based GUI Class cont'd.
#include <QQuickItem>
#include <QSGGeometry>
#include <QSGFlatColorMaterial>
public:
TriangleItem(QQuickItem *parent = 0);
protected:
QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *data);
private:
QSGGeometry m_geometry;
QSGFlatColorMaterial m_material;
};
45 © 2015
Exporting a Scene Graph based GUI Class cont'd.
#include "triangleitem.h"
#include <QSGGeometryNode>
TriangleItem::TriangleItem(QQuickItem *parent) :
QQuickItem(parent),
m_geometry(QSGGeometry::defaultAttributes_Point2D(), 3)
{
setFlag(ItemHasContents); m_material.setColor(Qt::red);
}
46 © 2015
Exporting a Scene Graph based GUI Class cont'd.
QSGNode *TriangleItem::updatePaintNode(QSGNode *n, UpdatePaintNodeData *)
{
QSGGeometryNode *node = static_cast<QSGGeometryNode *>(n);
if (!node) { node = new QSGGeometryNode(); }
QSGGeometry::Point2D *v = m_geometry.vertexDataAsPoint2D();
const QRectF rect = boundingRect();
v[0].x = rect.left();
v[0].y = rect.bottom();
v[1].x = rect.left() + rect.width()/2;
v[1].y = rect.top();
v[2].x = rect.right();
v[2].y = rect.bottom();
node->setGeometry(&m_geometry);
node->setMaterial(&m_material);
return node;
}
Demo: qml-cpp-integration/ex-simple-item-scenegraph
47 © 2015
Using Custom Types
Defining Custom Property Types
• Enums
• Custom types as property values
Timer {
id: timer
interval { duration: 2; unit: IntervalSettings.Seconds }
}
49 © 2015
Defining Custom Property Types
50 © 2015
Using Enums
class IntervalSettings : public QObject
{
Q_OBJECT
Q_PROPERTY( int duration READ duration WRITE setDuration
NOTIFY durationChanged )
Q_ENUMS( Unit )
Q_PROPERTY( Unit unit READ unit WRITE setUnit NOTIFY unitChanged )
public:
enum Unit { Minutes, Seconds, MilliSeconds };
Timer {
id: timer
interval {
duration: 2;
unit: IntervalSettings.Seconds
}
}
51 © 2015
Custom Classes as Property Types
private:
QTimer *m_timer;
IntervalSettings *m_settings;
}
52 © 2015
Custom Classes as Property Types cont'd.
53 © 2015
Custom Classes as Property Types cont'd.
54 © 2015
Custom Classes as Property Types cont'd.
Demo: qml-cpp-integration/ex_timer_custom_types
55 © 2015
Collections of Custom Types
Chart {
anchors.fill: parent
bars: [
Bar { color: "#a00000" value: -20 },
Bar { color: "#00a000" value: 50 },
Bar { color: "#0000a0" value: 100 }
]
}
• A chart item
• With a bars list property
• Accepting custom Bar items
Demo: qml-cpp-integration/ex-custom-collection-types
56 © 2015
Declaring the List Property
public:
ChartItem(QQuickItem *parent = 0);
void paint(QPainter *painter);
QQmlListProperty<BarItem> bars();
…
}
57 © 2015
Declaring the List Property
private:
static void append_bar(QQmlListProperty<BarItem> *list, BarItem *bar);
QList<BarItem*> m_bars;
58 © 2015
Defining the Getter Function
QQmlListProperty<BarItem> ChartItem::bars()
{
return QQmlListProperty<BarItem>(this, 0, &ChartItem::append_bar,
0, 0, 0);
}
59 © 2015
Defining the Append Function
60 © 2015
Summary of Custom Property Types
61 © 2015
Default Property
62 © 2015
Plug-ins
Creating Extension Plugins
64 © 2015
Defining an Extension Plugin
#include <QQmlExtensionPlugin>
public:
void registerTypes(const char *uri);
};
65 © 2015
Implementing an Extension Plugin
#include “ellipseplugin.h”
#include “ellipseitem.h”
66 © 2015
Building an Extension Plugin
TEMPLATE = lib
CONFIG += qt plugin
QT += quick
DESTDIR = ../plugins
67 © 2015
Using an Extension Plugin
68 © 2015
Using an Extension Plugin
69 © 2015
Loading an Extension Plugin
70 © 2015
Using an Extension Plugin
Item {
Ellipse {
x: 50; y: 50
width: 200;
height: 100
}
}
71 © 2015
Summary of Extension Plugins
72 © 2015
Lab – Chat Program
Lab: qml-cpp-integration/lab-tcp-conection
73 © 2015