2014-10-06 61 views
2

我有一個QTableView組件顯示行中的幾種類型的數據。我需要的是以不同顏色顯示的每一行。我的樣式表看起來是這樣的: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?

請問有沒有人知道一個很好的簡單的解決方案?

致以親切的問候和非常感謝。

回答

1

你不需要樣式表來做到這一點,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 

這裏有很多方法,但我會告訴你只有油漆,因爲它是你最重要的事情。關於另一個方法說明,你可以找到在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

+0

謝謝,這就是我一直在尋找的東西。它的工作原理! – klasyc 2014-12-10 08:18:46

0

雖然QItemDelegate是一個不錯的選擇,有時它只是一個過度殺死時,所有的喲你想做的是顏色一些細胞。 你可以簡單地這樣做:

QStandardItem *item = new QStandardItem(""); 
item->setData(Qt::gray, Qt::BackgroundColorRole); 

這完全適用於我。