我希望创建一个包含100个textedit的qml窗口作为示例,如何在循环中创建它?这可能吗?
答案 0 :(得分:11)
循环是命令式代码,所以它不是QML而是Javascript或C ++。当然,你可以这样做(例如通过在JS循环中嵌入一个Qt.createComponent()调用),但在QML中,认为声明是更好的事情,这意味着你不做'事',你'定义'事情:
import QtQuick 2.0
Rectangle {
id: base;
width: 400;
height: 800;
Column {
spacing: 5; // a simple layout do avoid overlapping
Repeater {
model: 10; // just define the number you want, can be a variable too
delegate: Rectangle {
width: 200;
height: 20;
color: "white";
border { width: 1; color: "black" }
radius: 3;
TextInput {
anchors.fill: parent;
}
}
}
}
}
从QML的角度来看,这种方式真的更强大,更清晰!
答案 1 :(得分:5)
查看QML Repeater元素http://doc.qt.io/qt-4.8/qml-positioners.html