How can I change QTableView's color of the area that doesn't have cells on it?
-
wrote on 27 Aug 2011, 16:20 last edited by
How can be changed QTableView's color of the area that doesn't have cells on it?
Thanks in advance!
-
wrote on 27 Aug 2011, 16:30 last edited by
Could you be more precise ? What do you want to change exactly ? The header's color ?
-
wrote on 27 Aug 2011, 16:34 last edited by
Set the background color. You can do that either with setting the palette, or by using a style sheet.
-
wrote on 27 Aug 2011, 23:13 last edited by
This snippet does the trick:
@
QTableWidget tw;
QPalette p = tw.palette();
p.setColor(QPalette::Base, QColor("yellow"));
tw.setPalette(p);
@Or set the respective color in the palette property in Qt Designer, if you're using .ui files.
-
wrote on 28 Aug 2011, 12:20 last edited by
Volker, I've tried like that but it also does changing the cells color.
-
wrote on 28 Aug 2011, 12:31 last edited by
[quote author="Edico" date="1314534038"]Volker, I've tried like that but it also does changing the cells color.[/quote]
That's correct. The background color is behind the cells and behind the widget :-(
What you could do is use a style sheet and set the background color of the table there and another background color for the cells. -
wrote on 28 Aug 2011, 17:54 last edited by
I didn't made it even with style sheets. I've reached doing something like that:
@
QTableView {
alternate-background-color: blue;
background: red
}QTableView::item:selected {
background-color: green
}QTableView::item {
background-color: white
}
@All items are white with no alternate row colors.
-
wrote on 28 Aug 2011, 18:32 last edited by
Are you using some kind of special, non-standard style?
-
wrote on 28 Aug 2011, 19:00 last edited by
Andre, no, I don't think so. With the above code I see al items white and the area that doesn't have cells on is red, but I see no alternating row colors (one white - one blue).
-
wrote on 29 Aug 2011, 12:04 last edited by
I can reproduce this problem here and it looks like a bug. When setting the background-color for the item in addition to the background -color and the alternate-background-color for the table, then the alternate-background-color will be ignored. I suggest you report this issue in "Jira":https://bugreports.qt.nokia.com//secure/Dashboard.jspa.
-
wrote on 29 Aug 2011, 13:29 last edited by
if you set the color for cells in general and also alternate background color, then the cell wins, that makes sense. and where else will you see the alternate background color?
-
wrote on 29 Aug 2011, 13:38 last edited by
Is there a way to have a color for the outside area of the cells, another for cells and other color for alternate background color?
-
wrote on 29 Aug 2011, 15:02 last edited by
you will not have alternate color if you set cells color via style sheet. alternate color is a cell color.
-
wrote on 29 Aug 2011, 15:30 last edited by
So, the only way is to subclass QSqlRelationalTableModel (the model I use to access data)? That looks like a big pain.
-
wrote on 29 Aug 2011, 17:48 last edited by
if you want alternate row colors and a different cell background then empty background? I think yes. Or use a proxy model instead of a subclass, that's much less work.
-
wrote on 30 Aug 2011, 06:51 last edited by
I can't figure how to use a proxy model for this job.
-
wrote on 30 Aug 2011, 06:56 last edited by
if you use a proxy model, the proxy models data method is called for each data query instead of the methods of the original model. This then calls the original (source) model. So you can do everything you want there, like changing backroundColor via the Qt::BackgroundRole. IMHO ProxyModels are easier then derived SQL models ;-)
-
wrote on 1 Sept 2011, 06:27 last edited by
Gerolf, this mean that I must subclass QSortFilterProxyModel and reimplement data() method?
-
wrote on 1 Sept 2011, 06:57 last edited by
Yes, you can create your custom ProxyModel by sublcassing QSortFilterProxyModel and reimplementing the data() method. From there, you can do everything you want, like Gerolf said.
Finally, you just set the sourceModel of your ProxyModel, and it should work :)
-
wrote on 1 Sept 2011, 06:59 last edited by
Yes. If you were using 4.8, I would suggest you use -QIdentityModel- [[doc:QIdentityProxyModel]] as your base class, but if you are still on 4.7, use [[doc:QSortFilterProxyModel]] instead. You only need to reimplement the data() method. In that method, you only need to implement the case where the Qt::BackgroundRole is requested. Based on the row number (odd or even), you can return a different color. For all other cases, you get the parent QModelIndex by calling mapToSource() on the model index, and then return the result of calling the data() method on the source model with the same role and the model index you just mapped.
2/36