Set color to QTableView string

void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){ model = new QSqlQueryModel(this); model->setQuery(sql); } 

With this method, I can install QSQlQueryModels in my QTableviews.

But how can I set the color to a string based on the value of the cell?

+6
source share
2 answers

The view draws a background based on the Qt::BackgroundRole cell role, which is the QBrush value returned by QAbstractItemModel::data(index, role) for this role.

You can subclass QSqlQueryModel to override data() to return the calculated color, or if you have Qt> 4.8, you can use QIdentityProxyModel

 class MyModel : public QIdentityProxyModel { QColor calculateColorForRow(int row) const { ... } QVariant data(const QModelIndex &index, int role) { if (role == Qt::BackgroundRole) { int row = index.row(); QColor color = calculateColorForRow(row); return QBrush(color); } return QIdentityProxyModel::data(index, role); } }; 

And use this model in the view, with the sql model specified as the source with QIdentityProxyModel::setSourceModel .

OR

You can leave the model unchanged and change the background using a set of delegates in the view using QAbstractItemView::setItemDelegate :

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

Since the latter method is not always obvious for translating from C ++ code, here is what is equivalent in python:

 def initStyleOption(self, option, index): super(BackgroundColorDelegate,self).initStyleOption(option, index) option.backgroundBrush = calculateColorForRow(index.row()) 
+18
source

It is best to define a custom model (subclass of QAbstractTableModel ). You probably want to have QSqlQueryModel as a member of this custom class.

If this is a read-only model, you need to implement at least these methods:

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

and for well-made models also

  QVariant headerData(int section, Qt::Orientation orientation, int role) const; 

If you need a model for editing / sending data, everything becomes a little more complicated, and you also need to implement these methods:

  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()); 

What really changes the appearance of the string in the return value of this method:

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

Mute example:

  QVariant MyCustomModel::data(const QModelIndex &index, int role) const { if ( !index.isValid() ) return QVariant(); int row = index.row(); int col = index.column(); switch ( role ) { case Qt::BackgroundRole: { if(somecondition){ // background for this row,col is blue return QVariant(QBrush (QColor(Qt::blue))); } // otherwise background is white return QVariant(QBrush (QColor(Qt::white))); } case Qt::DisplayRole: { // return actual content for row,col here, ie. text, numbers } case Qt::TextAlignmentRole: { if (1==col) return QVariant ( Qt::AlignVCenter | Qt::AlignLeft ); if (2==col) return QVariant ( Qt::AlignVCenter | Qt::AlignTrailing ); return QVariant ( Qt::AlignVCenter | Qt::AlignHCenter ); } } } 
+3
source

Source: https://habr.com/ru/post/913585/


All Articles