QAbstractTableModel 中的自定义编辑器
有谁有在 QAbstractTableModel 中使用 QWidget 作为编辑器的示例吗?
我有一个列,编辑时应创建一个带有选项列表的 QCombobox。
文档似乎建议我需要编写一个 QAbstractItemDelegate 和一个自定义绘制函数,但这似乎在 Qt::EditRole 中简单地弹出一个标准的 QCombobox 太过分了。
注意 - 每行的组合框内容都相同,仅当有人单击单元格时才需要显示。
我知道这应该很简单,但我无法让它发挥作用。对于基于 QTableWidget 的表来说很容易 - 但我需要它来处理非常大的数据表。
Does anyone have an example of using a QWidget as an editor in a QAbstractTableModel?
I have a column which when edited should create a QCombobox with the list of choices.
The docs seem to suggest I need to write a QAbstractItemDelegate and a custom paint function but that seems overkill to simply pop-up a standard QCombobox in Qt::EditRole.
Note - the combo box contents are the same for every row and it only needs to be shown when somebody clicks in the cell.
I know this should be simple but I can't get it to work. It's easy for a QTableWidget based table - but I need it for a very large data table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不需要走那么远。一种方法是子类化
QStyledItemDelegate
,然后重写createEditor ()
以便它返回您预先填充的组合框。如果您使用基本的 Qt 值类型,它的setEditorData
和setModelData
函数可能已经足够了。如果您需要跨多种不同模型工作的更通用的东西,您可以创建一个
QItemEditorFactory< /code>
将您的编辑器与正确的类型相关联。这也适用于自定义类型。
当您的视图的
EditTrigger
指示时,您的视图将获取特定于调用编辑的单元格的委托,并调用 delegate->createEditor(...) ,然后根据选项调整组合框的大小 参数以及将当前条目设置为模型指定的值,尽管其中大部分应该由QStyledItemDelegate
处理。因此,您不必直接担心Qt::EditRole
,因为视图将处理它。You don't need to go that far. One way is to subclass
QStyledItemDelegate
and then overridecreateEditor()
so that it returns your prepopulated combo box. ItssetEditorData
andsetModelData
functions will probably already suffice if you`re using basic Qt value types.If you need something more generic that works across many different models, you can create a
QItemEditorFactory
that associates your editor with the correct type. This also works well with custom types.When indicated by your view's
EditTrigger
, your view will get the delegate specific to the cell on which the edit is being invoked and calldelegate->createEditor(...)
which can then size the combo box according to theoptions
parameter as well as set the current entry to the value specified by the model, although most of this should be handled by theQStyledItemDelegate
. Thus, you won't have to worry about theQt::EditRole
directly as the view will handle that.您是否尝试查看 Qt 中的以下示例:
Spin Box 委托示例< /a>
也许它会让你对这个主题有更清晰的认识!
希望它有一点帮助!
Did you try and have a look at the following example from Qt :
Spin Box Delegate Example
Maybe it will give you a much clearer view on the subject !
Hope it helps a bit !