QAbstractTableModel 中的自定义编辑器

发布于 2024-08-30 10:16:17 字数 309 浏览 4 评论 0原文

有谁有在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-09-06 10:16:17

文档似乎建议我需要编写一个 QAbstractItemDelegate 和一个自定义绘制函数,但这似乎在 Qt::EditRole 中简单地弹出一个标准 QCombobox 太过分了。

你不需要走那么远。一种方法是子类化 QStyledItemDelegate ,然后重写 createEditor () 以便它返回您预先填充的组合框。如果您使用基本的 Qt 值类型,它的 setEditorDatasetModelData 函数可能已经足够了。

如果您需要跨多种不同模型工作的更通用的东西,您可以创建一个 QItemEditorFactory< /code>将您的编辑器与正确的类型相关联。这也适用于自定义类型。

当您的视图的 EditTrigger 指示时,您的视图将获取特定于调用编辑的单元格的委托,并调用 delegate->createEditor(...) ,然后根据选项调整组合框的大小 参数以及将当前条目设置为模型指定的值,尽管其中大部分应该由 QStyledItemDelegate 处理。因此,您不必直接担心 Qt::EditRole ,因为视图将处理它。

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.

You don't need to go that far. One way is to subclass QStyledItemDelegate and then override createEditor() so that it returns your prepopulated combo box. Its setEditorData and setModelData 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 call delegate->createEditor(...) which can then size the combo box according to the options parameter as well as set the current entry to the value specified by the model, although most of this should be handled by the QStyledItemDelegate. Thus, you won't have to worry about the Qt::EditRole directly as the view will handle that.

顾忌 2024-09-06 10:16:17

您是否尝试查看 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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文