당신은이 작업을 수행 할 스타일 시트를하지 않아도은 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);
결과 :

고맙습니다.이게 내가 찾고 있던 것입니다. 그리고 그것은 작동합니다! – klasyc