Book Image

Application Development with Qt Creator - Second Edition

Book Image

Application Development with Qt Creator - Second Edition

Overview of this book

Table of Contents (20 chapters)
Application Development with Qt Creator Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Code interlude – Qt Quick and the QML syntax


Most of the programming you do at the lowest level is imperative: you describe how an algorithm should work (take this value and square it, search for the first occurrence of this string and replace it, format this data this way, and so forth). With Qt Quick, your programming is largely declarative: instead of saying how, you say what. For example, in C++ with Qt, we might write code like this to draw a rectangle:

QRect r(0, 0, 16, 16);
QPainter p;
p.setBrush(QBrush(Qt::blue));
p.drawRect(r);

This code creates a rectangle of 16 x 16 pixels, allocates a QPainter object that does the drawing, tells the painter that its brush should be colored blue, and then tells the painter to draw the rectangle. In QML, we'd simply write the rectangle as follows:

import QtQuick 2.3
Rectangle {
    width: 16
    height: 16
    color: "blue"
}

The difference is obvious: we're just saying that there is a blue rectangle that's 16 x 16 pixels. It's up to the Qt Quick runtime...