QTableView 行样式

kla*_*syc 2 c++ qt qtableview qtstylesheets qt5.2

我有一个 QTableView 组件,在行中显示多种类型的数据。我需要的是用不同的颜色显示每种类型的行。我的样式表如下所示:

RecordSheet::item {
        border: 0px;
        color: black;
        padding: 1px 0px 0px 3px;
}
RecordSheet::item:selected, RecordSheet::item:selected:!active {
        background-color: #e8b417;
        color: black;
}
Run Code Online (Sandbox Code Playgroud)

我有两个想法如何实现这一目标:

  1. 使用data()模型中的方法并响应Qt::BackgroundColorRole。不幸的是,当我这样做时,背景颜色将被忽略,直到我border: 0px;从样式表中删除背景颜色,并且当我删除边框时,styleshhet 的填充将被忽略。奇怪的...

  2. 为每种类型的行设置 CSS/QSS 类,并在样式表中设置它们的颜色。然后使用模型为每种类型的行分配适当的类。所以样式表看起来像这样:

    RecordSheet::item {
        border: 0px;
        color: black;
        padding: 1px 0px 0px 3px;
    }
    RecordSheet::item[class=green_row] {
            background-color: green;
    }
    RecordSheet::item[class=red_row] {
            background-color: red;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我更喜欢这种方法,因为它将内容与外观分开,但我不知道该怎么做。也许使用 ItemDelegate?

请问,有人知道一个好的简单的解决方案吗?

亲切的问候和非常感谢。

Che*_*byl 5

你不需要样式表来做到这一点,styleshhet 并没有那么强大来完成开发人员想要的所有事情。使用更强大的东西——委托。我将向您展示主要思想和工作示例。标题:

#ifndef ITEMDELEGATEPAINT_H
#define ITEMDELEGATEPAINT_H

#include <QStyledItemDelegate>

class ItemDelegatePaint : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit ItemDelegatePaint(QObject *parent = 0);
    ItemDelegatePaint(const QString &txt, QObject *parent = 0);


protected:
    void paint( QPainter *painter,
                const QStyleOptionViewItem &option,
                const QModelIndex &index ) const;
    QSize sizeHint( const QStyleOptionViewItem &option,
                    const QModelIndex &index ) const;
    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget * editor, const QModelIndex & index) const;
    void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
    void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;

signals:

public slots:

};

#endif // ITEMDELEGATEPAINT_H
Run Code Online (Sandbox Code Playgroud)

这里有很多方法,但我只会向您展示绘画,因为这对您来说是最重要的。关于您可以在网络上找到的另一种方法的描述

程序文件:

void ItemDelegatePaint::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QString txt = index.model()->data( index, Qt::DisplayRole ).toString();

    if(index.row() == 0)//green row
        painter->fillRect(option.rect,QColor(0,255,0));
    else
        if(index.row() == 1)//blue row
            painter->fillRect(option.rect,QColor(0,0,255));
    else
        if(index.row() == 2)//red row
            painter->fillRect(option.rect,QColor(255,0,0));
    //and so on

    if( option.state & QStyle::State_Selected )//we need this to show selection
    {
        painter->fillRect( option.rect, option.palette.highlight() );
    }


    QStyledItemDelegate::paint(painter,option,index);//standard processing
}
Run Code Online (Sandbox Code Playgroud)

用法:

ui->tableView->setItemDelegate(new ItemDelegatePaint);
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述