QTableView 行中带有图标

发布于 2024-10-29 17:58:27 字数 821 浏览 6 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

机场等船 2024-11-05 17:58:27

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

看看优秀的 Qt 文档,我建议您在模型中覆盖它:

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

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

枚举 Qt::ItemDataRole
模型中的每个项目都有一组
与其关联的数据元素,每个
有着自己的角色。角色被使用
通过视图向模型指示
它需要哪种类型的数据。风俗
模型应该返回这些中的数据
类型。

Qt::DecorationRole :要处理的数据
以装饰形式呈现
的一个图标。 (QColor、QIcon 或 Qpixmap)

因此,您需要做的是在 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:

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

There are various roles (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)

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

盛装女皇 2024-11-05 17:58:27

将您的项目的DecorationRole设置为您想要的QPixmap,它应该可以工作。

编辑:

我猜图标取决于数据类型列中的值。

int rowCount = serendibMsgTableModel->rowCount();

for(int row = 0; row < rowCount; row++)
{
    QModelIndex index = serendibMsgTableModel->index(row, 1);
    QVariant value = serendibMsgTableModel->data(index);
    static QPixmap s_invalidIcon(PATH_TO_INVALID_ICON);
    static QPixmap s_type1Icon(PATH_TO_TYPE1_ICON);
    static QPixmap s_type2Icon(PATH_TO_TYPE2_ICON);

    QPixmap icon(s_invalidIcon);

    if(value.toString() == "type1")
    {
        icon = s_type1Icon;
    }
    else if(value.toString() == "type2")
    {
        icon = s_type2Icon;
    }
    serendibMsgTableModel->setData(index, icon, Qt::DecorationRole);
}

像这样的东西应该有效。
在 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.

int rowCount = serendibMsgTableModel->rowCount();

for(int row = 0; row < rowCount; row++)
{
    QModelIndex index = serendibMsgTableModel->index(row, 1);
    QVariant value = serendibMsgTableModel->data(index);
    static QPixmap s_invalidIcon(PATH_TO_INVALID_ICON);
    static QPixmap s_type1Icon(PATH_TO_TYPE1_ICON);
    static QPixmap s_type2Icon(PATH_TO_TYPE2_ICON);

    QPixmap icon(s_invalidIcon);

    if(value.toString() == "type1")
    {
        icon = s_type1Icon;
    }
    else if(value.toString() == "type2")
    {
        icon = s_type2Icon;
    }
    serendibMsgTableModel->setData(index, icon, Qt::DecorationRole);
}

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.

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