pyside更改QTreeWidgetItem的颜色

时间:2015-11-04 16:47:05

标签: python pyside qtreewidgetitem

如何在pyside中更改QTreeWidgetItem的颜色?我想要一些是红色的,一些是蓝色的。我尝试过各种各样的东西,但还没有找到合适的解决方案。希望有人能提供帮助。 感谢

enter image description here

# Modules
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore, QtSvg

# widget
# ------------------------------------------------------------------------------
class Example(QtGui.QWidget):

    def __init__(self,):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        # formatting
        self.setGeometry(300, 300, 250, 200)
        self.setWindowTitle("Example")

        # widgets
        self.itemList = QtGui.QTreeWidget()
        self.itemList.setItemsExpandable(True)
        self.itemList.setAnimated(True)
        self.itemList.setItemsExpandable(True)
        self.itemList.setColumnCount(2)
        self.itemList.setHeaderLabels(['', ''])

        # add items
        item0 = QtGui.QTreeWidgetItem(self.itemList, ['testing'])
        item1 = QtGui.QTreeWidgetItem(self.itemList, ['testing'])

        # add children
        for i in xrange(0,4):
            item00 = QtGui.QTreeWidgetItem(item0, ["Name"])

        # layout
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.addWidget(self.itemList)
        self.show()

# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:6)

item1.setForeground(0,QtGui.QBrush(QtGui.QColor("red")))