我正在尝试将一个动态实例化的QML组件添加到视图中,因为我需要将它添加到某个索引并显示在顶部。组件是正确创建的,但是当它被添加到模型中时它不会显示,如何解决?这是我的代码: main.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 1920
height: 1080
visible: true
Timer{
interval: 700
repeat: true
running: true
property int counter : -1;
onTriggered: {
counter++;
var component;
var sprite;
var object;
component = Qt.createComponent("Recta.qml");
// sprite = component.createObject(project, {"x" : 100, "y" : -300, "name" : "Joshua" + counter});
object = component.createObject({"name" : "*****" + counter})
project.insert(0, object)
}
}
ListModel{
id: project
}
ListView{
id: lview
width: 310
height: 1080
}
}
recta.qml
import QtQuick 2.0
Item{
width: 300
height: 300
property string name;
Rectangle{
width: 300
height: 300
color: "teal"
Text{
anchors.centerIn: parent
text: name
}
}
}
答案 0 :(得分:0)
这里修复了main.qml代码。
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 1920
height: 1080
visible: true
Timer{
interval: 700
repeat: true
running: true
property int counter : -1;
onTriggered: {
counter++;
project.insert(0, {"name":"*****" + counter});
}
}
ListModel{
id: project
}
ListView{
id: lview
width: 310
height: 1080
model: project
delegate: Recta {
name: model.name
}
}
}