Error: Qt.createQmlObject(): failed to create object: Non-existent attached object
-
I have a QVariantMap which i use to create children of GridLayout
my code:
import QtUtils 1.0 import QtQuick 2.5 import QtQuick.Controls 1.4 import QtQuick.Layouts 1.2 Item { Connections { target: model onModelChanged: updateSummaryList() } function updateSummaryList() { for(var i = summaryGrid.children.length; i > 0 ; i--) { console.log("destroying: " + i) summaryGrid.children[i-1].destroy() } try { for (var prop in model.map) { var labelText = Qt.createQmlObject('import QtQuick 2.5;Text { text: "first text:"; color: "white"; horizontalAlignment: Text.AlignRight; Layout.alignment: Qt.AlignRight; }', summaryGrid, prop+"label"); var valueText = Qt.createQmlObject(' import QtQuick 2.5;\ Text { text: "text"; color: "white";}', summaryGrid, prop+"value"); } } catch (error) { console.error(error); } } GridLayout { id: summaryGrid Component.onCompleted: updateSummaryList() columns: 2 } }
When i run this code i get:
Error: Qt.createQmlObject(): failed to create object: field1label:1:102: Non-existent attached object
What may be the reason?
-
A ListView appears to be a better match than the more complicated GridLayout and Repeater.
ListView { delegate: Item { width: ListView.view.width height: childrenRect.height Text { anchors.right: parent.horizontalCenter text: modelData.firstPart + ":" } Text { anchors.left: parent.horizontalCenter text: modelData.secondPart } } }
-
The only attached object is
Layout
so it's probably the reason to fail: QML engine does not see theGridLayout
when creating your object.Rather than fixing this, though, I'd recommend using a completely different approach:
- use
Repeater
in yourGridLayout
to create the items you need - pass the model you have to
Reapeater
so that it updates automatically (there will be no more any need forConnections
component) - do not create components in QML dynamically
This should greatly simplify the logic.
- use
-
@sierdzio said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:
- use
Repeater
in yourGridLayout
to create the items you need
But Repeater supports only one Component as delegate. I want it to fill 2 columns of GridLayout with 2 Text delegates.
Static grid that i am trying to make dynamic:
GridLayout { id: grid columns: 2 Text { text: "first text:"; color: "white"; horizontalAlignment: Text.AlignRight Layout.alignment: Qt.AlignRight } Text { text: "text"; color: "white"} Text { text: "second text:"; color: "white"; horizontalAlignment: Text.AlignRight Layout.alignment: Qt.AlignRight } Text { text: "text text"; color: "white"} Text { text: "the third text:"; color: "white"; horizontalAlignment: Text.AlignRight Layout.alignment: Qt.AlignRight } Text { text: "text text text"; color: "white"} }
Its output:
first text: text second text: text text the third text: text text text
- use
-
@Kyeiv said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:
@sierdzio said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:
- use
Repeater
in yourGridLayout
to create the items you need
But Repeater supports only one Component as delegate. I want it to fill 2 columns of GridLayout with 2 Text delegates.
Wrap the 2 delegates in a single component and use the
index
property to decide which to use for each cell. - use
-
@jeremy_k yes but still if so, i cannot take advantage of the index property when i use one object, let's say of index 1 to fill 2 Texts
i need something like:
GridLayout { columns: 2 Repeater { model: model.map delegate: Text { text: modelData.label horizontalAlignment: Text.AlignRight Layout.alignment: Qt.AlignRight } Text { text: modelData.value } } }
-
@jeremy_k
what i have: a map of objects which have 2 attributes - label and value.
what i want to achieve: the formatted list of them in form of:first label text: value text second label text: value text the third label text: its value text
so its centered, the " : " in the middle.
-
@Kyeiv said in Error: Qt.createQmlObject(): failed to create object: Non-existent attached object:
GridLayout {
columns: 2
Repeater {
model: model.map
delegate: Text {
text: modelData.label
horizontalAlignment: Text.AlignRight
Layout.alignment: Qt.AlignRight
}
Text {
text: modelData.value
}
}
}Then just put them in one delegate item:
GridLayout { columns: 2 Repeater { model: model.map delegate: Column { Text { text: modelData.label horizontalAlignment: Text.AlignRight Layout.alignment: Qt.AlignRight } Text { text: modelData.value } } } }
-
A ListView appears to be a better match than the more complicated GridLayout and Repeater.
ListView { delegate: Item { width: ListView.view.width height: childrenRect.height Text { anchors.right: parent.horizontalCenter text: modelData.firstPart + ":" } Text { anchors.left: parent.horizontalCenter text: modelData.secondPart } } }