从下面的文档,你可以看到一些代码片段演示了如何创建一个简单的ListView.
http://doc-snapshot.qt-project.org/qdoc/qml-qtquick-listview.html#model-prop
我把这些组装起来,放在一个演示程序中。在我的项目中,目录结构如下:
listview1$ tree . ├── imports │ └── model │ ├── ContactModel.qml │ └── qmldir ├── run.sh └── test.qml
qmldir描述了从这个plugin暴露出来的module.
module model ContactModel 1.0 ContactModel.qml
import QtQuick 2.0 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.0 import model 1.0 ListView { width: 180; height: 200 model: ContactModel {} delegate: Text { text: name + ": " + number } }
qmake make
~/Qt5.2.0/5.2.0/gcc_64/bin/qmlscene -I ./imports ./test.qml