(已编辑)在原始问题中,我错误地假设 GridView 本机使用二维模型。事实上,它与其他 QML 类似,需要一个元素列表
意见。为了使问题和答案更容易理解,我更改了给定的代码
轻微地。此外,我根据答案添加了工作解决方案。
在主程序中,我定义了 QStandardItemModel 的实例:
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
QStandardItemModel* cppmodel = new QStandardItemModel();
for (int i=0; i<100; i++) {
QStandardItem* item = new QStandardItem(QString("%1").arg(i,2,10,QChar('0')));
cppmodel->appendRow(item);
}
然后,我将模型注册到 QML:
viewer.rootContext()->setContextProperty("cppModel",cppmodel);
QStandardItemModel 是一个表,不是吗?那么,我怎样才能写一个委托给
在简单的 GridView 中显示项目:
GridView {
model: cppModel
delegate: Rectangle {
Text { text: ??? } //WHAT MUST BE USED HERE ???
}
}
我是否必须使用命名角色,或者我可以只使用正确创建的索引吗?
也许它可以帮助你:
在 QML 中使用 QStandardItemModel https://web.archive.org/web/20140809004643/http://developer.nokia.com/community/wiki/Using_QStandardItemModel_in_QML
您也可以尝试这样的代码:
GridView {
anchors.fill: parent
model: cppModel
delegate: Rectangle {
Text {
text: display;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)