How to create mupltiple qml components in a loop

I want to create a qml window with 100 textedit for an example, how to create it in a loop? Is it possible?

+4
source share
2 answers

The loop is imperative code, so it is not QML, but Javascript or C ++. Therefore, you can do this (for example, by embedding a call to Qt.createComponent () in a JS loop), but in QML it is better to think of declarative, which means that you do not "do" things, you define 'things:

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; } } } } } 

Thus, it is really more powerful and much cleaner in terms of QML!

+10
source

Take a look at the QML Repeater element http://doc.qt.io/qt-4.8/qml-positioners.html

+5
source

All Articles