gpt4 book ai didi

qml - 如何在循环中创建多个 qml 组件

转载 作者:行者123 更新时间:2023-12-04 18:01:41 25 4
gpt4 key购买 nike

例如,我希望创建一个包含 100 个文本编辑器的 qml 窗口,如何在循环中创建它?那可能吗?

最佳答案

循环是命令式代码,所以它不是 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 的角度来看,它确实更强大,更干净!

关于qml - 如何在循环中创建多个 qml 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15474687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com