PyQt: reset or remove the background color from QTreeWidgetItem

moDong picture moDong · Jun 11, 2016 · Viewed 7.4k times · Source

How do I remove the background color of a QTreeWidgetItem, or reset it to the default?

treeWidgetItem.setBackgroundColor(0, QtGui.QColor('green'))

Answer

ShellRox picture ShellRox · Jun 12, 2016

I am not sure if there is any way of doing it with setBackgroundColor, But i would use setStyleSheet.

Stylesheet's work with every QtGui widget and are more easier to use overall.

If you want to set the QTreeWidget background color to green:

    self.TreeWidgetItem = QtGui.QTreeWidgetItem()
    self.TreeWidgetItem.setStyleSheet("background-color: green;")

If you want to reset the Stylesheet of QTreeWidget, simply type this:

    self.TreeWidgetItem.setStyleSheet("")

This would reset any widget's color to default one, without giving any exceptions.

Also it is good practice to use qt stylesheet system, It is easy and has a lot of perks.