1

I am developing a Qt application in C++. The application centers around a tree widget, which has a sub-tree for each Entity in the system. Entities have "highlight" colors, which can be specified by the user and highlight events related to a particular Entity. The Entity root tree item's background color is the highlight color for the Entity. When an event occurs related to the Entity, I'd like to make the background color of the identifier matched on the Entity the same as the Entity's highlight color. Here's a rough mock-up of what I'm trying to do, with pseudo-HTML tags showing the colors and where I want them:

<bgcolor=blue>Entity A</bgcolor>
    Events
        Identifier1 and <bgcolor=blue>Identifier2</bgcolor>
        <bgcolor=blue>Identifier2</bgcolor> @ 10:45:22
        <bgcolor=blue>Identifier3</bgcolor> and Identifier4

As I understand it, this is not directly possible with QTreeWidgetItem. I know that I can provide a delegate for the tree item to handle custom painting, but I am not well-versed in the QStyle part of Qt. Our customers will be using this application on XP and Win7, and may or may not have WindowsClassic style enabled. As such, handling all of the QStyle possibilities could become very complex. Is there a shortcut by which I can have QStyle draw the parts of the text that I want with the background colors that I want, without having to handle every potential piece of the tree item's drawing needs?

iamtheddrman
  • 623
  • 5
  • 16
  • stylesheets? http://stackoverflow.com/a/26128914/2266412 – Max Go Oct 02 '14 at 18:55
  • 1
    Basically you need to display HTML in Qt views. You can accomplish this by creating custom item delegate. See [this question](http://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt). – Pavel Strakhov Oct 03 '14 at 01:47
  • @PavelStrakhov, thanks! I'm not well-versed in HTML, but I finally found the tag and that did the trick for me, along with the linked question. If you submit it as an answer, I'll accept it. – iamtheddrman Oct 03 '14 at 18:56

1 Answers1

1

Basically you need to display HTML in Qt views. You can accomplish this by creating custom item delegate. See this question.

Setting color with HTML:

text1 <span style="color: #ff0000">red text</span> text2
Community
  • 1
  • 1
Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • This did the trick, but I did end up having to deal with Qt styles since I am now painting the tree item myself. Once I figured out the basics of drawing, I was able to get the rest of the style stuff done as well. My coworkers are very impressed with the results, so thanks! – iamtheddrman Oct 14 '14 at 15:12