gpt4 book ai didi

c++ - 带有行中图标的 QTableView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:12:51 25 4
gpt4 key购买 nike

我有一个显示数据库表行的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 我需要在“数据类型”列中显示图标。

希望这对您的回答有所帮助。

最佳答案

我看到您已经选择了一个答案,但由于您正在学习 Qt,我将添加一些内容。

查看出色的 Qt 文档,我建议您在模型中覆盖它:

QVariant QSqlTableModel::data ( 
const QModelIndex & index,
int role = Qt::DisplayRole ) const   [virtual]

有多种角色(int role = Qt::DisplayRole):

enum Qt::ItemDataRole : Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

Qt::DecorationRole : The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or Qpixmap)

因此,您需要做的是在 DisplayRole 的 data() 函数中返回一个 QIcon 或 QPixmap。

另一种可能更合适的方法是使用委托(delegate):例如ColorListEditor

关于c++ - 带有行中图标的 QTableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5535181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com