From below doc, you will see some code snippet which shows how to create a simple ListView.
http://doc-snapshot.qt-project.org/qdoc/qml-qtquick-listview.html#model-prop
I assemble them and put them in one demo app. In my project, the folder tree is:
http://doc-snapshot.qt-project.org/qdoc/qml-qtquick-listview.html#model-prop
I assemble them and put them in one demo app. In my project, the folder tree is:
listview1$ tree
.
├── imports
│ └── model
│ ├── ContactModel.qml
│ └── qmldir
├── run.sh
└── test.qml
This demo app contains one test.qml and another plugin which only contains one ContactModel.qml file without C++ dynamic library.
qmldir describes the module exposed from plugin
module modelThe ContactModel.qml is a model for ListView to provide data.
ContactModel 1.0 ContactModel.qml
import QtQuick 2.0In test.qml file, apply ContactModel to model attribute. The delegate is a way to iterate all ListElements provided by model and show each element as one Text block on Window.
ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
import QtQuick 2.0To build this, just call following commands:
import model 1.0
ListView {
width: 180; height: 200
model: ContactModel {}
delegate: Text {
text: name + ": " + number
}
}
qmakeTo run it:
make
~/Qt5.2.0/5.2.0/gcc_64/bin/qmlscene -I ./imports ./test.qml
No comments:
Post a Comment