PDA

View Full Version : Background color for rows in a QTableWidget: How to do that?



robgeek
8th May 2015, 04:43
Hello!

I have a QTableWidget in my program and i want to set a background rgb color for some rows, but i read QTableWidgetItem::setBackgroundColor is obsolete and i'm using Qt 5.4.1 with QtCreator 3.4.0. How can i do what i want? I really don't want to change to QTableView.

Thanks!

andsun
20th May 2015, 15:36
Hi, have the same problem

Hope for some answers too ;)

In my case I use QSortFilterProxyModel to first filter the data. This data can be further selected for use using a combobox
In my combobox the user can select options 1- 3 and that shall set background for a group of data in the tableview, meaning first to third group of 4 rows each.

Thinking of adding a column and data to filter like "group1", "group2", "group3" - is that a better idea?

any ideas?

/anders

d_stranz
20th May 2015, 21:13
These are two different questions. QTableView and QTableWidget use different methods for setting the background color for rows.

For QTableWidget, when you add or modify the QTableWidgetItem(s) in the rows, you set the color using QTableWidgetItem::backgroundBrush().

If you are using QTableView with a QAbstractItemModel (or proxy), you implement the Qt::BackgroundRole role in the QAbstractItemModel::data() method to set the QBrush used to fill the cells in the row with the appropriate color.

andsun
21st May 2015, 07:53
Hi d_strantz,

OK, good to know!
Give it a google & try ;)

andsun
22nd May 2015, 13:01
Thanks, that did the trick for me
/a