将颜色设置为QTableView行

将颜色设置为QTableView行,qt,qt4,qtableview,qsqltablemodel,Qt,Qt4,Qtableview,Qsqltablemodel,使用此方法,我可以将QSQlQueryModels设置为我的QtableView 但是如何根据单元格值设置行的颜色?最好的方法是定义一个自定义模型QAbstractTableModel子类。您可能希望将QSqlQueryModel作为此自定义类中的成员 如果是只读模型,则至少需要实现以下方法: void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){ mode

使用此方法,我可以将QSQlQueryModels设置为我的QtableView


但是如何根据单元格值设置行的颜色?

最好的方法是定义一个自定义模型QAbstractTableModel子类。您可能希望将QSqlQueryModel作为此自定义类中的成员

如果是只读模型,则至少需要实现以下方法:

void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){
        model = new QSqlQueryModel(this);
        model->setQuery(sql);
}
 QVariant headerData(int section, Qt::Orientation orientation, int role) const;
对于表现良好的模型也是如此

 int rowCount(const QModelIndex &parent) const;
 int columnCount(const QModelIndex &parent) const;
 QVariant data(const QModelIndex &index, int role) const;
如果您需要模型能够编辑/提交数据,事情会变得更加复杂,您还需要实现以下方法:

void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){
        model = new QSqlQueryModel(this);
        model->setQuery(sql);
}
 QVariant headerData(int section, Qt::Orientation orientation, int role) const;
实际改变行外观的是此方法的返回值:

 Qt::ItemFlags flags(const QModelIndex &index) const;
 bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
 bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
 bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()); 
一个愚蠢的例子:

 QVariant data(const QModelIndex &index, int role) const;

该视图基于单元格的Qt::BackgroundRole角色绘制背景,该角色是该角色的QbstractItemModel::dataindex角色返回的QBrush值

您可以将QSqlQueryModel子类化以重新定义数据以返回计算的颜色,或者如果您的Qt>4.8,则可以使用:

并在视图中使用该模型,将sql模型设置为带有QIdentityProxyModel::setSourceModel的源

或 您可以保持模型不变,并使用QAbstractItemView::setItemDelegate:


+1以供代表参考解决方案。我忘了。我需要为表格的每个值设置颜色colmun SELECT name,status FROM users在本例中status可以编辑此代码。optionV4->backgroundBrush=QBrushcalculateColorForRowindex.row;它产生error@Tineo我忘了计算颜色。。。函数必须是常量。要从模型中获取状态,可以使用index->siblingindex->row,1/*status*/的列,因此,可能需要将const QModelIndex&index而不是int row传递给函数。更改任何单元格颜色后,单元格不会自动更新/重绘。可以通过在QTableView上调用updateindex,或者通过从QTableView的子类发出或调用dataChanged index、index来强制更新。谢谢
class BackgroundColorDelegate : public QStyledItemDelegate {

public:
    BackgroundColorDelegate(QObject *parent = 0)
        : QStyledItemDelegate(parent)
    {
    }
    QColor calculateColorForRow(int row) const;

    void initStyleOption(QStyleOptionViewItem *option,
                         const QModelIndex &index) const
    {
        QStyledItemDelegate::initStyleOption(option, index);

        QStyleOptionViewItemV4 *optionV4 =
                qstyleoption_cast<QStyleOptionViewItemV4*>(option);

        optionV4->backgroundBrush = QBrush(calculateColorForRow(index.row()));
    }
};
def initStyleOption(self, option, index):
    super(BackgroundColorDelegate,self).initStyleOption(option, index)
    option.backgroundBrush = calculateColorForRow(index.row())