You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, August 14th 2009, 12:26pm

QTableWidget alternating row color for sorted column

Hi all !

I am trying to use QTableWidget to present some data with alternating row color. I used setAlternatingRowColors() and it worked fine, but now I want to use different alternating row colors for the column that the table is currently sorted by.

I gets ugly, because I can't find a way to get the order in which the rows are currently displayed!

The following method doesn't work in case we have several identical elements in a table:

Source code

1
2
3
4
5
6
7
8
9
10
void casino::colorSortedColumn(int col) {
     QColor color1 = QColor(192,200,202);
     QColor color2 = QColor(226,238,234);
     for (int i=0; i<ui->table->rowCount(); i++) {
         QTableWidgetItem *it = ui->table->item(i, col);
         if ( i % 2 == 0) it->setBackgroundColor(color1);
         else it->setBackgroundColor(color2);
     }
     return;
}


Please, help me!
Thaaanks!