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.0
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);
  QQuickView view;
  qmlRegisterType<CarInfo>("CarInfo", 1, 0, "CarInfo");
  view.setSource(QUrl("qrc://main.qml"));
  view.show();
  return app.exec();
}

In general this would work (yes, it is as simple as that). However, at the time of writing, trying ...

Get Game Programming Using Qt now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.