I have got a QTableView component displaying several types of data in the rows. What I need is to display each type of row by different color. My stylesheet looks like this:
RecordSheet::item {
border: 0px;
color: black;
padding: 1px 0px 0px 3px;
}
RecordSheet::item:selected, RecordSheet::item:selected:!active {
background-color: #e8b417;
color: black;
}
I have two ideas how to achieve this:
Use
data()
method in the model and respond to theQt::BackgroundColorRole
. Unfortunately when I do it, the background color is ignored until I remove theborder: 0px;
from the stylesheet and when I remove the border, the styleshhet's padding is ignored. Strange...Setup a CSS/QSS class for each type of row and set their colors in the stylesheet. Then use the model to assign a proper class for each type of row. So the stylesheet would look like this:
RecordSheet::item { border: 0px; color: black; padding: 1px 0px 0px 3px; } RecordSheet::item[class=green_row] { background-color: green; } RecordSheet::item[class=red_row] { background-color: red; }
I like this approach more because it separates content from the appearance, but I don't have any idea how to do it. Maybe using an ItemDelegate?
Please, does anybody know a nice and simple solution?
Kind regards and many thanks.
Jan