PDA

View Full Version : QTableWidget set Row Color



sagirahmed
19th October 2010, 10:15
Hi,

How to set the color of QTableWidget row. Want to set the color of specified row.

Thanks & Regards

aamer4yu
19th October 2010, 11:05
Iterate through the items of a given row and use QTableWidgetItem::setBackground

Or alternatively you can use delegates...

sagirahmed
19th October 2010, 11:28
Iterating take too much time to set row color. if I want to change the color of 1000000 nth item then iterating this take time and my program get slow. I need a function to change the row by passing only row Index.

I am using this code:
model->setData(model->index(Row,Column,QModelIndex()),QColor(Qt::red),Qt ::BackgroundRole);

But in this I required to go through all column for changing the same row color.

faldzip
19th October 2010, 11:48
Then get rid of QTableWidget, use QTableView and implement your own model, where you can store colors for each row, and return this color in data() for each column in row. You can add method to your model (it can be even slot) setRowColor(int row, const QColor &color) where you can set the color for row and emit dataChanged() signal for whole row at once.

sagirahmed
19th October 2010, 13:09
I am using QModelIndex to set the value. In QModelIndex we need to pass row and column number . I am searching a function that will take only row number in QModelIndex

faldzip
19th October 2010, 13:24
QModelIndex represents only one index (item), which is in particular row and column. If you want to treat row as a one object then make your own model and add functionality to it allowing you to operate on whole rows.

As I said in the previous post: Subclass QAbstractItemModel (or any other model class if you want their functionality) and keep there lets say a list (or maybe a map if you find that more suitable for your needs) of colors where color at position r is a color of row number r. Then in data() method return appropriate color for given index, where you can check only row of index. Then you can add some method e.g. setRowColor(int r, const QColor &c) where you can set color in a list, and update whole row at once by emitting QAbstractItemModel::dataChanged() covering indexes for whole row.