We have an existing QT project written in C++, and we want to add something like the mapViewer example project to our existing UI. We can't figure out how to instantiate the mapViewer, and invoke the method that initializes/displays it. Following online help, we came up with the function below, that returns a QQuickWidget which we can add to a UI element. We keep getting an error that the created component never gets to be ready, so the function does not work.
QQuickWidget *buildMap(QWidget *parent)
{
QQmlEngine *engine = new QQmlEngine;
QQmlComponent component(engine, "qrc://mapviewer.qml");
QObject *object = component.create();
QMetaObject::invokeMethod(object, "initializeMap");
QQuickWidget *map = new QQuickWidget(engine, parent);
return map;
}
It does not make a ton of sense to us that we are creating an object using the mapViewer component, and then just forgetting about it, but the examples we found online have a flow similar to this.
Try something like this instead:
As for the
initializeMap
method, maybe call it inmapviewer.qml
'sonCompleted
? If you insist on calling it from C++, you can still get to the object thorughmap->rootObject()
Also, when creating components and objects, be that from C++ or QML, it is always a nice idea to check the status and output error strings if any, you don't just assume it will all work and then wonder why it doesn't.