January 17, 2011

MasterMatrix MasterMatrix
Lab Rat
12 posts

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:

  1.   modelUI = new QSqlTableModel(this);
  2.     modelUI->setTable("test");
  3.     modelUI->setData(modelUI->index(3,3),
  4.                              qVariantFromValue(QColor(150, 50, 50)),
  5.                              Qt::BackgroundColorRole);
  6.     modelUI->setEditStrategy(QSqlTableModel::OnManualSubmit);
  7.     if (!modelUI->select()) {qDebug() << "Error select";}
  8.     ui->tableView->setModel(modelUI);

9 replies

January 17, 2011

Volker Volker
Robot Herder
5428 posts

You must set the background after the select!

Reading from the database with select destroys the previously read data (== indexes) and inserts new ones.

January 18, 2011

MasterMatrix MasterMatrix
Lab Rat
12 posts

Dear Volker!

You mean this?

  1. if (!modelUI->select()) {qDebug() << "Error select";}
  2. modelUI->setData(modelUI->index(3,3), QBrush(Qt::red), Qt::BackgroundColorRole);
  3. 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!

January 18, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 18, 2011

Volker Volker
Robot Herder
5428 posts

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.

January 18, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Or create a Proxy model, only overwrite the data method and there change the color.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 18, 2011

Volker Volker
Robot Herder
5428 posts
Gerolf wrote:
Or create a Proxy model, only overwrite the data method and there change the color.

That’s even better!

January 18, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 18, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

You could just use the QSortFilterProxyModel and overwrite the data method. No need to create a complete custom proxy model which handles persistent model indexes and all that stuff…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 18, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ Qt Designed and Grid Layout: how to span multiple columns/rows?      [Merged, Moved] Compile problems on Ubuntu and OS X ››

You must log in to post a reply. Not a member yet? Register here!