O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Making CarInfo instantiable from QML

First, we will update the QML document to create an instance of CarInfo present in the CarInfo 1.0 module:

import QtQuick 2.9
import CarInfo 1.0

Image {
    source: "dashboard.png"

    CarInfo {
        id: carData
        visible: true // make the widget visible
    }
  // ...
} 

As for registering CarInfo, it might be tempting to simply call qmlRegisterType on CarInfo and congratulate ourselves for a job well done:

int main(int argc, char **argv) {
    QGuiApplication app(argc, argv);    QQmlApplicationEngine engine;    // this code does not work
    qmlRegisterType<CarInfo>("CarInfo", 1, 0, "CarInfo");    //...
} 

In general, this would work (yes, it is as simple as that). However, it will not work with widgets. It's not possible ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required