Answer the question
In order to leave comments, you need to log in
How to arrange elements from ListModel (QML) in strictly defined places?
In general, the task is as follows: it is necessary to place elements in the program window (the interface is implemented through QML), in certain coordinates. The coordinates of the elements and their number at the initial moment are not known.
All element information is available through the ListModel.
In general, I solved the problem, something like this:
//main.qml
import QtQuick 2.3
Item {
id: item
width: 500
height: 500
function addItems() {
for (var i = 0; i < mainModel.count; i++) {
var component = Qt.createComponent("RectItem.qml");
if (component.status === Component.Ready) {
var childRec = component.createObject(item);
childRec.x = mainModel.get(i).x;
childRec.y = mainModel.get(i).y;
childRec.color = mainModel.get(i).color;
}
}
}
ListModel {
id: mainModel
ListElement {
x: 100
y: 110
color: "#123"
}
ListElement {
x: 450
y: 90
color: "#a63"
}
ListElement {
x: 90
y: 400
color: "#c13"
}
ListElement {
x: 120
y: 200
color: "#543"
}
ListElement {
x: 300
y: 177
color: "#fc3"
}
}
MouseArea {
id: mouse
anchors.fill: parent
onClicked: addItems()
}
}
// RectItem.qml
import QtQuick 2.0
Rectangle {
width: 50
height: 50
radius: 25
color: "#a33"
}
Answer the question
In order to leave comments, you need to log in
I didn't quite understand whether it is necessary to add elements one at a time. If not, then I would do this:
Repeater {
model: mainModel
anchors.fill: parent
Rectangle {
width: 100
height: 100
color: model.color
z: index
x: model.x
y: model.y
MouseArea {
anchors.fill: parent
onClicked: Qt.quit()
}
}
}
Repeater {
id: repeater
model: mainModel
anchors.fill: parent
property int counter: 0
Rectangle {
width: 100
height: 100
color: model.color
visible: index<=repeater.counter
z: index
x: model.x
y: model.y
MouseArea {
anchors.fill: parent
onClicked: {
repeater.counter++
}
}
}
}
The ability to formulate a question is already 70% success and an increase in the probability of getting an answer to almost one.
This question looks like: "what do you like least of all because of the day before yesterday?"
In general, if the model is not directly displayed unambiguously (a different number of elements, some special display criteria, etc.), then it is advisable to use one of the interlayer models (QIdentityProxyModel, QSortingProxyModel, etc.)
If you formulate your question more Specifically, it will be easier to answer in detail.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question