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->setTable("test");
- modelUI->setData(modelUI->index(3,3),
- if (!modelUI->select()) {qDebug() << "Error select";}
- ui->tableView->setModel(modelUI);
9 replies
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 [doc.qt.nokia.com], 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.
You must log in to post a reply. Not a member yet? Register here!


