I have a custom QML type written in C++, the classname is MyCustomType and it's located in the files mycustomtype.h and mycustomtype.cpp.
In the main.cpp file the QML type is made available:
qmlRegisterType<MyCustomType>("MyCustomType", 1, 0, "MyCustomType");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("../qml/main.qml")));
In the main.cpp file I can get access to the engine's root object like this:
rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
My issue is that I need access to that rootObject from within the MyCustomType class in the mycustomtype.cpp file. Is that possible?
The only way I could think of was to pass the rootObject to the constructor. But since the MyCustomType is instatiated in the QML document, and not in C++ code, this solution won't work.
Any ideas?
QQuickItem
you can useQQuickItem::window()
to access the window where the item is rendered. – GrecKo