void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){
model = new QSqlQueryModel(this);
model->setQuery(sql);
}
With this method i can set a QSQlQueryModels to my QTableviews.
But How i can set color to a row based on a cell value?
The view draws the background based on the
Qt::BackgroundRolerole of the cell which is theQBrushvalue returned byQAbstractItemModel::data(index, role)for that role.You can subclass the
QSqlQueryModelto redefinedata()to return your calculated color, or if you have Qt > 4.8, you can use aQIdentityProxyModel:And use that model in the view, with the sql model set as source with
QIdentityProxyModel::setSourceModel.OR
You can keep the model unchanged and modify the background with a delegate set on the view with
QAbstractItemView::setItemDelegate:As the last method is not always obvious to translate from C++ code, here is the equivalent in python: