如何使用Qt的Model-View编程
我正在尝试将一些卡片显示到 QListView 中,但我真的很难理解如何使用 Qt 的模型/视图模式,而且我找不到任何简单的示例。
基本上,我有两个类:
Card - 我的“模型”,其中包含卡的名称、id 等。
CardWidget - 可以加载和渲染 Card 对象(显示卡名称和其他信息)
那么我如何使用 Card 和CardWidget 将卡片列表显示到 ListView 中?我是否需要对我的类进行某些更改,或者 QListView 是否应该能够直接显示它们?
如果有人可以向我展示基本步骤或为我指明正确的方向,那就完美了。
I'm trying to display some cards into a QListView but I'm really having trouble understanding how to use Qt's model/view pattern, and I can't find any simple examples.
Basically, I have two classes:
Card - my "model" which contains the name of the card, id, etc.
CardWidget - can load and render a Card object (display the card name and other info)
So how can I use Card and CardWidget to display a list of cards into a ListView? Do I need to change something to my classes, or should QListView be able to display them directly?
If someone could show me the basic steps or point me in the right direction that would be perfect.
请参阅 QAbstractItemDelegate 的文档,其中有一个在 QTableView 中渲染项目的示例。
您在这里尝试执行的操作并不明显 - 在列表视图中,您可以呈现项目的视图,该视图与每个单元格中都有一个小部件不同。
项目委托可以提供一个小部件作为编辑器以及如何呈现单元格的内容。
如果您确实想要在视图中使用固定小部件,则可以在您想要固定小部件的所有单元格上使用 QListView::openPersistentEditor。项目委托应概述如何为相关单元格创建编辑器。
See the documentation of QAbstractItemDelegate, which has an example of rendering items in a QTableView.
Its not obvious what you are trying to do here - in a list view, you can render a view of an item which is not the same as having a widget in every cell.
An item delegate can provide a widget as an editor and also how to render a cell's contents.
If you actually want fixed widgets in the view, you could use QListView::openPersistentEditor on all the cells you want a fixed widget for. The item delegate should outline how to create an editor for the cell in question.