I am trying to set the individual colors of each line in a QtreeWidget, I have tried setting them manually as I fill the treeView with QBrush(col, QColor(R, G, B, a))
and without the alpha value. I have also tried filling out the treeView in PyQT6 Designer, and it looks fine in the designer, but when I export the .ui file and import it, the colors don’t show up (I have even checked the .ui XML data, and I can see the color property in the file), is this an issue with QtreeWidget, or am I doing something wrong?
import sys
from PyQt6.uic import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
class MyApp(QMainWindow):
def __init__(self):
super(MyApp, self).__init__()
loadUi("Test.ui", self)
if __name__ == '__main__':
app = QApplication(sys.argv)#[])
window = MyApp()
window.show()
app.exec()
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTreeWidget" name="treeWidget">
<column>
<property name="text">
<string>1</string>
</property>
</column>
<item>
<property name="text">
<string>Test1</string>
</property>
<property name="foreground">
<brush brushstyle="NoBrush">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</property>
</item>
<item>
<property name="text">
<string>Test2</string>
</property>
<property name="foreground">
<brush brushstyle="NoBrush">
<color alpha="255">
<red>0</red>
<green>170</green>
<blue>0</blue>
</color>
</brush>
</property>
</item>
<item>
<property name="text">
<string>Test3</string>
</property>
<property name="foreground">
<brush brushstyle="NoBrush">
<color alpha="255">
<red>0</red>
<green>85</green>
<blue>255</blue>
</color>
</brush>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Marshall Haynes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.