简体   繁体   中英

How can you change the background or text color for part of a QTreeWidgetItem

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM