c++ - QTableView 行样式

标签 c++ qt qtableview qt5.2 qtstylesheets

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

RecordSheet::item {
        border: 0px;
        color: black;
        padding: 1px 0px 0px 3px;
}
RecordSheet::item:selected, RecordSheet::item:selected:!active {
        background-color: #e8b417;
        color: black;
}

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

  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;
    }
    

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

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

亲切的问候和非常感谢。

一月

最佳答案

您不需要样式表来执行此操作,styleshhet 并没有那么强大,无法完成开发人员想要的所有事情。使用更强大的东西——委托(delegate)。我将向您展示主要思想和工作示例。 header :

#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

这里有很多方法,但我只向您展示绘画,因为这对您来说是最重要的。关于您可以在 web 中找到的其他方法的说明

cpp:

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
}

用法:

ui->tableView->setItemDelegate(new ItemDelegatePaint);

结果:

enter image description here

关于c++ - QTableView 行样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214741/

相关文章:

c++ - 如何将各种类型的函数指针存储在一起?

c++ - 如何在 QTableView 中排序和更改日期格式

c++ - Qt - 禁用 QDialog 的 "?"按钮

c++ - 如何在 QTableView/QAbstractTableModel 中使用多行文本/换行符?

c++ - QtableView 将整行设置为只读

c++ - 定义仅由某个类需要的结构的最佳方法是什么?

java - 追加现有数组

c++ - 为什么 IHTMLImgElement 方法 put_src 在从线程调用时返回 E_OUTOFMEMORY?

qt - QStandardItemModel & Qtableview

c++ - 有谁知道 QT (4.7) 头文件和库在 mac 上的位置(即路径)?