Alternate-background-color
-
Hi,
I have a QTableView.
I want to color a row with a jump of 2 rows.
So i'd like to have:WHITE ROW
WHITE ROW
RED ROW
WHITE ROW
WHITE ROW
RED ROW
...... and so onHow can i do this with a stylesheet?
I have this piece of code but it doesn't work obviously.
QTableView {
alternate-background-color: red;
background: white;
}Thanks in advance.
-
Hi and welcome to devnet,
AFAIK, you can't do it like that through style sheet, however you can use a QIdentityProxyModel .
Hope it helps
-
Thank you for the useful information.
How can I use QIdentityProxyModel?
I read many websites but still can't to solve this problem.I'm using a QStandardItemModel with the QTableView
-
Subclass it, implement the data function to return the correct color for the Qt::BackgroundRole
Then
@
YourModel *model = new YourModel;
YourIdentityProxyModel * proxyModel= new YourIdentityProxyModel;
proxyModel->setSourceModel(model);
yourView->setModel(proxyModel);
@ -
Something like this?
@
class MyProxyModel : public QIdentityProxyModel
{
public:
QVariant data(const QModelIndex &index, int role){
if (role == Qt::BackgroundRole){
QColor color = Qt::red;
return QBrush(color);
}
}
return QIdentityProxyModel::data(index, role);
}
@ -
You need to add the math for your custom color schema
-
Maybe you don't need the proxy model here. You can treat the BackgroundColor role directly in your original model...
-
@SGaist How do I add the math?
-
@ Vinorcola the idea behind the proxy is that if you use the same model on another view you don't necessarily want to have that kind of colors so you don't modify the original data and use the proxy between the view where you actually want to have that new background.
@SoQT you want me to write for you the logic that returns white for the first row and red for the 2 next ?