Model BackgroundRole overridden by style sheet
-
Hello,
I have a custom model which sets the BackgroundRole display role to a red color when a certain item has an error associated with it. This works well except when I apply a style sheet to the List View displaying the model. Once the style sheet is applied, it appears to override any of the display role settings (i.e. no more red color).
Is there a way to make sure (a) that the display role returned by the model is applied or (b) that I can apply a style sheet selector based upon the display role to set the color there?
Thanks
-
Hi,
What aspects are you currently styling through stylesheets ?
-
@SGaist This is the stylesheet I am applying to the ListView widgets.
QListView::item { border-bottom: 1px solid black; color: black; height: 30px; } QListView::item:selected { background: #e6e6e6; } QListView::item:hover { background: #f2f2f2; }
-
That's imo exactly what's described here: https://bugreports.qt.io/browse/QTBUG-70100
I'm unsure if this should really be the case because the style sheet wins by definition and this would be an exception. Don't set the background in the style sheet and return the proper color directly in the model is imho the way to go. -
That's imo exactly what's described here: https://bugreports.qt.io/browse/QTBUG-70100
I'm unsure if this should really be the case because the style sheet wins by definition and this would be an exception. Don't set the background in the style sheet and return the proper color directly in the model is imho the way to go.@Christian-Ehrlicher It does like that request refers to the same issue. The only odd this is, if I remove all of the
"background" items in the style sheet (just preserving the size and border changes), I still loose the color set by the model. This leads me to believe that, if any style sheet is in affect, Qt simply ignores all the formatting coming from the model.If this is the case, is there any way I could have the model apply a property or something similar to the data I want colored, then use a style sheet selector to change the background colors that way?
-
If there is no background property set I would expect that the background from the model is used - if this is not the case it's imo a bug.
-
Ok, in the case that this really is a bug, I will take step back (since this bug will most likely not get fixed any time soon) to explain what I would like to accomplish. Perhaps there is a different solution.
I have a model which represents objects that can be in different states. I want the views (list, table, etc...) attached to these models to change their row background colors based upon the state of those objects. For example, when an object is in an error condition, the corresponding row in the model should become red. Returning the correct color in the data() method of the model based upon the role is a good solution, however I also want to easily be able to change these colors WITHOUT having to modify the code and recompile, hence why I was trying to use style sheets.
I suppose one alternative would be to use settings which the model uses when returning the color. I like the style sheet idea because it easily allows me to make "themes" for the application without complicated code for that purpose.
Any suggestions to accomplish this goal?
-
@Christian-Ehrlicher It does like that request refers to the same issue. The only odd this is, if I remove all of the
"background" items in the style sheet (just preserving the size and border changes), I still loose the color set by the model. This leads me to believe that, if any style sheet is in affect, Qt simply ignores all the formatting coming from the model.If this is the case, is there any way I could have the model apply a property or something similar to the data I want colored, then use a style sheet selector to change the background colors that way?
@Patrick-Wright said in Model BackgroundRole overridden by style sheet:
This leads me to believe that, if any style sheet is in affect, Qt simply ignores all the formatting coming from the model.
I believe this is correct. I think the model's hint goes through the palette, while the stylesheet disables the palette.
-
I'm not sure if this can help you since I don't know if this will work for items: http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties
Wrt to the possible bug - please create a bug report so is not forgotten. -
Hi
Could poster not use a QStyledItemDelegate ?
with void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
implementation. ? -
Ok, in the case that this really is a bug, I will take step back (since this bug will most likely not get fixed any time soon) to explain what I would like to accomplish. Perhaps there is a different solution.
I have a model which represents objects that can be in different states. I want the views (list, table, etc...) attached to these models to change their row background colors based upon the state of those objects. For example, when an object is in an error condition, the corresponding row in the model should become red. Returning the correct color in the data() method of the model based upon the role is a good solution, however I also want to easily be able to change these colors WITHOUT having to modify the code and recompile, hence why I was trying to use style sheets.
I suppose one alternative would be to use settings which the model uses when returning the color. I like the style sheet idea because it easily allows me to make "themes" for the application without complicated code for that purpose.
Any suggestions to accomplish this goal?
@Patrick-Wright
Now that you have explained what you're trying to achieve, I'd like to second @Christian-Ehrlicher's post above and suggest you look at that Qt "dynamic properties" link. It is a fact that stylesheets override code palette colors which I think is whatBackgroundRole
etc. use. I use dynamic properties at present to "attach" certain style rules to widgets. Like him I'm not sure if/how you can get this to work from the model cells to the view, but it's worth a thought.... -
This post is deleted!