Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: QTableWidget/QTableView alternating row colors in full viewport

I have a QTableView containing data rows from a database. However, setting setAlternatingRowColors(true) only alternates row colors that has data - the rest of the table is just white, which is not the behaviour you'd expect (look in the bookmark list of any browser, for example - empty rows has alternating colors).

Does anyone know a workarund or an alternative to the table views supplied by Qt? I've fiddled with stylesheets and item delegates, same result.

like image 366
Zumteufel Avatar asked Mar 16 '13 17:03

Zumteufel


2 Answers

Why don't you use Qt QSS for this? It working just fine. Have a look here : http://www.qtcentre.org/threads/42211-QTableWidget-alternating-background-color?p=263046#post263046

myTable->setAlternatingRowColors(true);
myTable->setStyleSheet("alternate-background-color: yellow;background-color: red;");
like image 186
swdev Avatar answered Oct 18 '22 20:10

swdev


You could reimplement data() method of your model like this:

QVariant MyModel::data(const QModelIndex& index, int role) const
{
    if(role == Qt::BackgroundColorRole)
        return color;
    ...
}

It should be possible to do the same with a delegate using setModelData().

like image 40
Funt Avatar answered Oct 18 '22 19:10

Funt