2014-10-06 2 views
2

행에 여러 유형의 데이터를 표시하는 QTableView 구성 요소가 있습니다. 내가 필요한 건 행의 각 유형 을 표시하는 것입니다. 내 스타일은 다음과 같습니다 : 모델에QTableView 행 스타일링

  1. 사용 data() 방법과 Qt::BackgroundColorRole에 응답 : 나는 이것을 달성하는 방법이 개 아이디어를 가지고

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

    . 불행하게도 스타일 시트에서 border: 0px;을 제거 할 때까지 배경색이 무시되고 테두리를 제거하면 스타일 시트의 패딩이 무시됩니다. 이상한 ...

  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

0

QItemDelegate 때때로, 좋은 옵션이지만 그 이상 그냥 죽일 때 모든 요 당신이하고 싶은 것은 어떤 세포에 색을 칠하는 것입니다. 당신은 단순히이 작업을 수행 할 수 있습니다 이것은 나를 위해 완벽하게 작동

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

.