Important: Please read the Qt Code of Conduct -

Custom Stylesheeting and custom painting

  • My application uses an external stylesheet for skinning. There is a default *.qss file that's compiled with the app's resources, and users should be able to replace it with their own.

    In my app, I have a QListView that I custom draw with my own item delegate. I'd like to provide for an "odd/even" item row background. I'd like the colors of these backgrounds to be determined in the *.QSS file.

    any ideas if this is at all possible? It's like I need access to custom fields in my QSS file from my paint method on my item delegate. Of course, I can open that file and analyze its contents myself, but i'd much rather use a systemic approach to this, if one exists of course.

  • There is no Qt API to do this, unfortunately. The whole stylesheet based styling is a bit of a blackbox, and can not be combined with custom painting easily. There is no public API for accessing the evaluated results of a style sheet for an item that needs to be drawn. I fear that analyzing the contents of the file yourself (or, perhaps easier, setting these values separately if you don't want to have to write a complete CSS parser) is your only way out.

  • Hi,
    I'm pretty much a newbie still but have been spending a lot of time looking at examples and remembered this one: "Style Sheet Example":
    Maybe it could give you some ideas.

    In one of the style sheets: coffee.qss
    They specify the bq. “odd/even” item row background
    @QTreeView {
    alternate-background-color: yellow;

  • Thanks. I do want it to be part of my CSS. I'll write a simple parser that just looks for the name of the QListView it's called for (e.g. "QListView#<myObjectName>" and parse the values below to fish for the odd/even background values.

    There is no need to open the file itself I think, since I set the stylesheet to the QApplication, I believe I can get it as a string, and just parse the string. Will check.

  • Hi Paucoma, this is custom drawing using an item delegate so in any case, I need to look for those properties and apply them myself.

  • Fine, if it works for you...
    Just realize that if your user is allowed to modify the CSS used, he might use other selectors. He might not use the name of your list view for instance, but want to apply the style, or only this part, to all list views in the application. Then your custom parsing would fail, and your users might (rightly) start to complain about a bug. The solution is a bit fragile, it seems. But if you are happy with it, then that is obviously the most important thing :-)

  • I believe I misled you, by "user" I meant our own marketing/sales who will re-brand our software as needed. I'm not talking about the end user.

  • [quote author="ronM71" date="1300810954"]I believe I misled you, by "user" I meant our own marketing/sales who will re-brand our software as needed. I'm not talking about the end user.[/quote]
    Ah, ok. In that case, I guess it is a question of properly documenting this, so you, your successor and the marketing department will not be surprised by this issue :-)

  • If you use the "QStyledItemDelegate": you get colors set by the stylesheet in the paint event. And if you call the default methods, the style sheet drawing is used.

    I'm using this for changing some smaller things in painting...

Log in to reply