Using Loader as a TableView delegate either fails or throws a warning in 6.5.0
-
I have a delegate that looks like this:
```delegate: Loader {
sourceComponent: { if (model === undefined || model === null || parent === null) { return } var component_qml = "import QtQuick; Component { Rectangle { border.color: 'black';" switch(column){ case 0: component_qml += " Text{padding: 10; text: eks_name}"; break; . . . } component_qml += "}}" return Qt.createQmlObject(component_qml, parent) } }With 6.5.0 that throws the error: qt.qml.typecompiler: qrc:/...../Pages/inline:1:17: Using a Component as the root of a qmldocument is deprecated: types defined in qml documents are automatically wrapped into Components when needed. If I remove the Component I get the following error: qrc:/......./Pages/EksPage.qml:46:17: Unable to assign QQuickRectangle to QQmlComponent -
Hello!
It seems you are trying to bind the "object" obtained by Qt.createQmlObject() to a QQmlComponent property: this behavior is only allowed if the created object is a Component{}.
On the other hand, it seems that the deprecation warning makes no sense in the case of code used by Qt.createQmlObject(), I will remove it.
In general, generating qml code at runtime is a bad idea and should be avoided. I think you might check https://doc.qt.io/qt-6/qml-qt-labs-qmlmodels-delegatechooser.html out as an alternative.
-
It seems that Qt.createQmlObject() internally makes use of QQmlComponent, such that the deprecated case of having a QQmlComponent in a QQmlComponent really happens (at least internally).
Another alternative might be to first create a component, and then to use the setData()-slot of the component to set the qml-source code. That would avoid creating one extra QQmlComponent and triggering the warning:
let comp = Qt.createComponent("QtQuick", "Item"); comp.setData(component_qml); // component_qml without the Component