Hello,

I'm struggling with dynamic loading of *.qml files. Although my real application is way bigger I have managed to reduce the problem to following code:

(1) Single c++ application loading main.qml and then loading WindControlItem.qml

Qt Code:
  1. /*
  2.  * main.cpp
  3.  */
  4. #include <QGuiApplication>
  5. #include <QQmlApplicationEngine>
  6. #include <QQuickItem>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  11. QGuiApplication app(argc, argv);
  12. QQmlApplicationEngine engine;
  13. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  14. if (engine.rootObjects().isEmpty())
  15. return -1;
  16.  
  17. // Root object for finding QML Items by their objectName
  18. QObject* root = engine.rootObjects()[0];
  19. assert(root != nullptr);
  20.  
  21. // The QML Item to which we want to inject our QML-Item-loaded-from-file
  22. QQuickItem* centralPane = qobject_cast<QQuickItem*>(root->findChild<QObject*>("centralPane"));
  23. assert(centralPane != nullptr);
  24.  
  25. // Load the QML file to a component
  26. QString qml_path = "WeatherCtrlItem.qml";
  27. QQmlComponent comp(&engine, QUrl::fromLocalFile(qml_path));
  28.  
  29. // Create an instance of the component
  30. QObject* obj = comp.create();
  31. assert(obj != nullptr);
  32.  
  33. // Up-cast it to a QQuickItem
  34. QQuickItem *item = qobject_cast<QQuickItem*>(obj);
  35. assert(item != nullptr);
  36.  
  37. // Born C++, die QML
  38. engine.setObjectOwnership(item, QQmlEngine::JavaScriptOwnership);
  39.  
  40. // Set QObject-parent to the central pane
  41. item->setParent(centralPane);
  42.  
  43. // Set Visual-parent to the central pane
  44. item->setParentItem(centralPane);
  45.  
  46. return app.exec();
  47. }
To copy to clipboard, switch view to plain text mode 

(2) Content of main.qml

Qt Code:
  1. /*
  2.  * main.qml
  3.  */
  4.  
  5. import QtQuick 2.10
  6. import QtQuick.Controls 1.4
  7.  
  8. ApplicationWindow {
  9. id: root
  10. visible: true
  11. width: 800
  12. height: 600
  13.  
  14. menuBar: MenuBar {
  15. Menu {
  16. title: "File"
  17. MenuItem {
  18. text: "Exit"
  19. onTriggered: Qt.exit(0)
  20. }
  21. } // Menu
  22. } // MenuBar
  23.  
  24. Rectangle {
  25. anchors.left: parent.left
  26. width: (parent.width * 0.2)
  27. height: parent.height
  28. }
  29.  
  30. // I want this item to eventually become a parent of another dynamically loaded
  31. // item -- that's why I give it an objectName.
  32. Rectangle {
  33. objectName: "centralPane"
  34. width: parent.width * 0.6
  35. height: parent.height
  36. anchors.horizontalCenter: parent.horizontalCenter
  37. }
  38.  
  39. Rectangle {
  40. width: (parent.width * 0.2)
  41. height: parent.height
  42. anchors.right: parent.right
  43. }
  44.  
  45. } // ApplicationWindow
To copy to clipboard, switch view to plain text mode 

(3) Content of WindControlItem.qml

Qt Code:
  1. /*
  2.  * WindControlItem.qml
  3.  */
  4. import QtQuick 2.10
  5. import QtQuick.Controls 1.4
  6.  
  7. Rectangle {
  8. id: recRoot
  9. color: "#778899"
  10. anchors.fill: parent
  11.  
  12. GroupBox {
  13. id: gpbLayers
  14. title: "Wind layers"
  15. anchors.fill: parent
  16.  
  17. Grid {
  18. id: grdFields
  19. anchors.fill: parent
  20. columns: 2
  21.  
  22. Text {
  23. text: "speed"
  24. }
  25.  
  26. TextField {
  27. id: fieldSpeed
  28. placeholderText: "meters/sec"
  29. }
  30.  
  31. Text {
  32. text: "direction"
  33. }
  34.  
  35. TextField {
  36. id: fieldDir
  37. placeholderText: "degrees"
  38. }
  39.  
  40. Button{
  41. text: "Add layer"
  42. }
  43.  
  44. } // Grid
  45.  
  46. } // GroupBox
  47.  
  48. } // Item root
To copy to clipboard, switch view to plain text mode 

When I start the application the GUI displays correctly. The problem is that after I close the ApplicationWindow lots of warnings are printed to the output:

Qt Code:
  1. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/GroupBox.qml:184: ReferenceError: parent is not defined
  2. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/GroupBox.qml:204: ReferenceError: parent is not defined
  3. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/GroupBox.qml:205: ReferenceError: parent is not defined
  4. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/GroupBox.qml:206: ReferenceError: parent is not defined
  5. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/GroupBox.qml:216: ReferenceError: parent is not defined
  6. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/Control.qml:90: ReferenceError: parent is not defined
  7. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/AbstractCheckable.qml:123: ReferenceError: parent is not defined
  8. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/Control.qml:90: ReferenceError: parent is not defined
  9. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/TextField.qml:656: ReferenceError: parent is not defined
  10. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/TextInputWithHandles.qml:104: ReferenceError: parent is not defined
  11. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/TextInputWithHandles.qml:131: ReferenceError: parent is not defined
  12. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/Control.qml:90: ReferenceError: parent is not defined
  13. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/TextField.qml:656: ReferenceError: parent is not defined
  14. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/TextInputWithHandles.qml:104: ReferenceError: parent is not defined
  15. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/TextInputWithHandles.qml:131: ReferenceError: parent is not defined
  16. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/Control.qml:90: ReferenceError: parent is not defined
  17. file:///C:/tools/Qt/5.12.0/msvc2017/qml/QtQuick/Controls/Private/BasicButton.qml:194: ReferenceError: parent is not defined
To copy to clipboard, switch view to plain text mode 

I have tried to debug the code with both Qt 5.11.3 and Qt 5.12.0 but the resulting behavior is identical.


Finally my questions:

(1) Am I using correct approach for dynamic loading of Items defined in *.QML file?

(2) What could be the cause for the reference errors?


Than you for your answers!