Important: Please read the Qt Code of Conduct -

How to display rich text with icon in ItemView items?

  • I want to display formatted rich text (with full Qt-HTML support i.e. <font>, <b>, etc. as supported by QLabel) along with a leading or trailing icon inside an item in QTreeView. I have gone through many posts but could not find a guideline to implement in full generality which also requires stylesheet support i.e.
    QTreeView::item { background-color: ... ; font-family: .. } etc.

    Note: I need both the stylesheet properties provided and the individual items properties. How do I go about doing it using item delegates?

  • Lifetime Qt Champion

    The delegate can only support a stylesheet if you use QStyle() paint functions.
    Sadly its not possible to query stylesheet properties so its not that easy to support Stylesheet also using pure QPainter.

    For the richtext, this can be used as a base.

  • So basically there is no Qt API to extract those values from the stylesheet applied to the QTreeView? I cannot hard-code any values, because stylesheet will come from a different source, which I will not have control over (what properties are set). Does this imply I have to write my own CSS parser? Does Qt really not have a unified way of getting the style sheet properties in the paint method of item delegate?

  • Lifetime Qt Champion

    Nope. No Api to read that.
    And while you can reuse Qt parser its not fun

    But using QStyle paint function the values are used but you can still not read them or
    make logic based on them.

  • Thank you for the gist @mrjj . Any idea why Qt API is designed to not give access to QSS properties as an API? or why is the parser not exposed with public API? It means the entire requirement of writing the drawing logic by reconciling item properties + stylesheet becomes needlessly complicated now since Qt by default does not support rich text in items. This is quite unexpected from Qt, since it covers so much .

  • Lifetime Qt Champion

    I can't say why it was never made. Would surely make custom widget even more powerful.
    I guess the logic is that style sheet would allow to style std. items and Delegate allows for truly
    custom drawing.

    does allow to read/modify some of the values from stylesheet but not directly.

    So the best option would to use QStyle drawing to use as many values as possible but its not optimal.

Log in to reply