C++ (Qt) if (index.row() == <номер строки>) { painter->fillRect(rect, QColor(Qt::green)); } else { QItemDelegate::paint(painter, option, index); }
C++ (Qt) if (index.column() == 7 && index.data( Qt::DisplayRole ).toString() == "") { painter->fillRect(option.rect, QColor(Qt::red)); } else if (index.column() == 8 && index.data( Qt::DisplayRole ).toString() == "") { painter->fillRect(option.rect, QColor(Qt::green)); } else { QItemDelegate::paint(painter, option, index); }
C++ (Qt)class MyDelegate : public QItemDelegate{private: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;};
C++ (Qt) MyDelegate *mDeleg = new MyDelegate; dbView->setItemDelegate(mDeleg);
C++ (Qt)void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ if (index.sibling(index.row(),7).data( Qt::DisplayRole ).toString() == "") { painter->fillRect(option.rect, QColor(Qt::red)) } else if (index.sibling(index.row(),8).data( Qt::DisplayRole ).toString() == "") { painter->fillRect(option.rect, QColor(Qt::green)); } else { QItemDelegate::paint(painter, option, index); }}
C++ (Qt)void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ if (index.sibling(index.row(),7).data( Qt::DisplayRole ).toString() == "") { painter->fillRect(option.rect, QColor(Qt::red)); } else if (index.sibling(index.row(),8).data( Qt::DisplayRole ).toString() == "") { painter->fillRect(option.rect, QColor(Qt::green)); } QItemDelegate::paint(painter, option, index);}
bool QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) [virtual]Sets the role data for the item at index to value. Returns true if successful; otherwise returns false.
C++ (Qt)model->setData(model->index(5,5),QColor(Qt::red),Qt::BackgroundRole); dbView->setModel(model); dbView->setSelectionBehavior(QAbstractItemView::SelectRows); dbView->show();
C++ (Qt)class mySqlQueryModel : public QSqlQueryModel{ Q_OBJECTpublic: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;};
C++ (Qt)QVariant mySqlQueryModel::data(const QModelIndex &index, int role) const{ QVariant value = QSqlQueryModel::data(index, role); if(role == Qt::BackgroundColorRole) { if (index.sibling(index.row(),7).data( Qt::DisplayRole ).toString() == "" && index.sibling(index.row(),8).data( Qt::DisplayRole ).toString() == "") { return qVariantFromValue(QColor(Qt::darkRed)); } else if (index.sibling(index.row(),7).data( Qt::DisplayRole ).toString() != "" && index.sibling(index.row(),8).data( Qt::DisplayRole ).toString() == "") { return qVariantFromValue(QColor(Qt::darkGreen)); } else return value; } else return value;}