PDA

View Full Version : [QT4] Models and display roles - setting background color?



KShots
10th March 2006, 21:28
Should I be looking at roles under the model's data() function to be able to set a background color for a given index? Or should I be looking elsewhere for such?

Basically, I have a 'status' column, where I will have both text and a background color reflecting the status (red and green). Any ideas? Nothing I saw jumped out at me in the docs, unfortunately.

Ben.Hines
10th March 2006, 21:37
The QAbstractItemModel::data() method accepts an ItemDataRole enum value. You would need to override this and key off of Qt::BackgroundColorRole in order return the background color that you want. I think it is kind of strange for the view to ask the model for the color, but that's neither here nor there.

Alternatively, you could implement your own item delegate that does custom painting and set it on the QAbstractItemView using the setItemDelegate method. This seems like a lot of work if you're just changing the background color of a column or row.

You could also use the QTableWidget, which acts more like the Qt3 table and insert QTableWidgetItem's (you can set the background color on these directly).

Hope this helps.

KShots
10th March 2006, 21:46
Thanks. I see what they did in the docs - I didn't realize they divided the tables like that. I looked at the top table of the screen and didn't see the lower tables were dealing with the same data. Thanks for the tip, that got it working great :).