如何在 QAbstractItemView 中显示操纵的模型内容
我有一个 QSqlTableModel
和一个 QTableView
显示模型的内容。
我成功地自定义了模型子类化 QStyledItemDelegate
的编辑,并重新实现了 setEditorData
、setModelData
和 updateEditorGeometry
方法。
我现在需要做的就是在视图中放入基于原始模型的文本。让我举一个简单的例子:
假设我在模型中存储了值“10”
。我想要的是在视图中显示该值的操纵版本,例如“10 kg”,但不更改模型的实际内容。
(这只是一个简化的示例,真正的问题不仅仅是添加后缀。)
我该怎么做? 我无法在文档中找到答案并查看 两个< /a> 示例 我可以找到与我的问题类似的内容,但它们处理新的表示形式,绘制为星星(例如)。我想要的更简单:我希望能够重写字符串。
抱歉英语很糟糕。
I have a QSqlTableModel
and a QTableView
displaying the contents of the model.
I've managed to customize the editing of models subclassing QStyledItemDelegate
and reimplementing the methods setEditorData
, setModelData
and updateEditorGeometry
.
What do I need to do now is put in the view a text based on the original model. Let me give a toy example:
Suppose I have stored the value "10"
in the model. What I want is to display a manipulated version of this value in the view, for example "10 kg"
, but without changing the actual content of the model.
(This was only a simplified example, the real problem is not just putting a suffix.)
How can I do this?
I could not find the answer in the documentation and looking at the two examples I could find similar to my problem, but they deal with new representation, drawn as stars (for example). What I want is simpler: I want to be able to rewrite the string.
Sorry about the crap english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于找到了答案。
事实上,答案就在文档中,但我没有注意到。只需重新实现方法
displayText
QStyledItemDelegate 的 >。这里是原始实现。很高兴看到它以了解如何进行修改。
要进行更复杂的操作,最好看一下方法
initStyleOption
(实现)。I finally managed to find an answer.
The truth is that the answer was in the documentation and I let it go unnoticed. Just reimplement the method
displayText
ofQStyledItemDelegate
.Here is the original implementation. It was good to see it to get an idea of how to do the modifications.
To do more complex manipulations, it is also good to take a look at the method
initStyleOption
(implementation).