QTableView 行中带有图标
我有一个 QTableView
显示数据库表的行。在此表中,我有一列称为数据类型,并且每种类型都有图标图像。如何在每种数据类型前面添加这些图标?
这是我的代码的一部分,应 justanothercoder 的要求。
QString msgQueryString = "select MESSAGE_ID, DATA_TYPE from SER_MESSAGES where MESSAGE_ID > 500 ";
serendibMsgTableModel->setQuery(msgQueryString, *database);
serendibMsgTableModel->setHeaderData(0, Qt::Horizontal, tr("Message ID"));
serendibMsgTableModel->setHeaderData(1, Qt::Horizontal, tr("Data Type"));
serendibMsgProxyModel->setSourceModel(serendibMsgTableModel);
serendibMsgView->setModel(serendibMsgProxyModel);
“serendibMsgTableModel”是一个QSqlQueryModel
,“serendibMsgProxyModel”是一个自定义的QSortFilterProxyModel
。 “serendibMsgView”是 QTableView
我需要在“数据类型”列中显示图标。
希望这对您的回答有所帮助。
I have a QTableView
showing rows of a database table. In this table I have a column called data type and I have icon images for each type. How can I add these icons in front of each data type?
Here's a part of my code as requested by justanothercoder.
QString msgQueryString = "select MESSAGE_ID, DATA_TYPE from SER_MESSAGES where MESSAGE_ID > 500 ";
serendibMsgTableModel->setQuery(msgQueryString, *database);
serendibMsgTableModel->setHeaderData(0, Qt::Horizontal, tr("Message ID"));
serendibMsgTableModel->setHeaderData(1, Qt::Horizontal, tr("Data Type"));
serendibMsgProxyModel->setSourceModel(serendibMsgTableModel);
serendibMsgView->setModel(serendibMsgProxyModel);
"serendibMsgTableModel" is a QSqlQueryModel
and "serendibMsgProxyModel" is a customized QSortFilterProxyModel
. "serendibMsgView" is the QTableView
I need the icons to be displayed, in the Data Type column.
Hope this helps for your answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到您已经选择了答案,但既然您正在学习 Qt,我将添加一些内容。
看看优秀的 Qt 文档,我建议您在模型中覆盖它:
有各种角色(int role = Qt::DisplayRole):
因此,您需要做的是在 DisplayRole 的 data() 函数中返回 QIcon 或 QPixmap。
另一种可能更合适的方法是使用委托:例如ColorListEditor
I saw that you've already picked an answer but since you are learning Qt I'll add a few things.
Taking a look at the excellent Qt documentation I suggest you overwrite this in your model:
There are various roles (int role = Qt::DisplayRole):
Thus, what you need to do is return a QIcon or QPixmap in the data() function for the DisplayRole.
Another approach which might be more appropriate is to make use of delegates: For example ColorListEditor
将您的项目的DecorationRole设置为您想要的QPixmap,它应该可以工作。
编辑:
我猜图标取决于数据类型列中的值。
像这样的东西应该有效。
在 setModel 之前设置值。
我还没有测试过,但我想你应该从中得到启发。
Set the DecorationRole of your items to the QPixmap you want and it should work.
edit:
I guess that the icon depends on the value in the data type column.
Something like this should work.
Set the values before setModel.
I haven't tested it, but I think you should get the idea from this.