PyQt 和 QML - 如何创建自定义数据模型
我应该如何在 PyQt 中创建 QAbstractListModel 并将其与 QML ListView 一起使用?
How should I create a QAbstractListModel in PyQt and use it with a QML ListView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果其他人正在寻找答案,我制作了一个小应用程序,它使用 QAbstractListModel 的子类连接到参与者数据库,并根据 @fgungor 发布的示例在 QGridView 中显示缩略图。(PyQt5)
main.py :
models.py:
db.py:
main.qml:
If someone else is looking for an answer, I made a small app that connects to a database of actors with with a subclass of QAbstractListModel and displays the thumbnails in QGridView based on the example @fgungor posted.(PyQt5)
main.py:
models.py:
db.py:
main.qml:
您需要设置它的角色名称才能在 QML 中使用它;
http://doc.qt.io/qt-4.8/qabstractitemmodel.html#setRoleNames
You need to set it's role names to be able to use it in QML;
http://doc.qt.io/qt-4.8/qabstractitemmodel.html#setRoleNames
还没有使用过 PyQT,但您可以在这里找到一个最小的工作示例:http://doc. qt.nokia.com/stable/qdeclarativemodels.html
如果您检查包含
class Animal {...}
的示例,您会发现必须为不同字段定义角色你想要供应。并且至少必须定义 data() 函数,返回给定索引的相应字段值。此外,您可能还需要自己的自定义方法来插入和删除。希望这有帮助...Haven't used PyQT but you can find a minimal working sample here: http://doc.qt.nokia.com/stable/qdeclarativemodels.html
If you inspect the sample including the
class Animal {...}
you'll see that you have to define roles for the different fields you want to supply. And at a minimum you must define the data() function returning the corresponding field value for a given index. Also you'll need your own custom methods for inserting and removing possibly. Hope this helps...