CellsColor in the QTableView
-
Hello!
It’s warking on Qt 4.5.3 and QtCreator 1.2.1
I try to change color of a background in a cell of model QTableView.
The code is compiled, but color of a background doesn't change. Help, please. A code:@ modelUI = new QSqlTableModel(this);
modelUI->setTable("test");
modelUI->setData(modelUI->index(3,3),
qVariantFromValue(QColor(150, 50, 50)),
Qt::BackgroundColorRole);
modelUI->setEditStrategy(QSqlTableModel::OnManualSubmit);
if (!modelUI->select()) {qDebug() << "Error select";}
ui->tableView->setModel(modelUI);@ -
Dear Volker!
You mean this?
@
if (!modelUI->select()) {qDebug() << "Error select";}
modelUI->setData(modelUI->index(3,3), QBrush(Qt::red), Qt::BackgroundColorRole);
ui->tableView->setModel(modelUI);@I'm sorry, but it doesnt work
If you mean something else will you please show me some simple example?
Thanks!
-
I doubt it is going to work at all, but I may be mistaken.
To me, it sounds strange to set data on a QSqlTableModel that will not and can not actually be stored in the underlying data store. At least; I assume your database does not actually store the color, does it?Still, perhaps QSqlTableModel is intelligent enough to cache this data for you.
-
MasterMatrix, it's possible that the SQL models use lazy loading and the model is still not filled when you set the background.
Better way to achieve your goal would be to subclass "QStyledItemDelegate":http://doc.qt.nokia.com/stable/qstyleditemdelegate.html, reimplement the paint method, set the background there and call the base class' implementation for the rest.
-
I use the last way too. I like the separation of concerns that you get here:
- base model contains the actual data
- proxy model handles decorating the data, such as changing colors
- view handles actual displaying of the data
I think I have a proxy model lying around somewhere that can be used for this. If I find it, I'll let you know. I think it was limited to table-like base models (no trees), but that should not matter as the base model was not a tree anyway.
-
Of course. QSFPM is a proxy model as well, right?
If you need a complicated proxy model, depends on what you want to achieve exactly. If you want to color a complete row or column, then that is easy to do with QSFPM. If you want to be able to manually color cells, you need a bit more than that.