Q:

How can i apply different background colors to each row of the parent text in tree widget using pyqt4

Here in my sample code,i want to apply the different colors for parent text of the each row in tree widget,How can i apply the background color to parent text row,i tried with Qbrush but i didn't get expected output.SocCan any one please tell me. Thank you in advance.
Given below is my sample code:
from PyQt4 import QtCore, QtGui
class InventoryDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        super(InventoryDelegate, self).paint(painter, option, index)
        if not index.parent().isValid():
            painter.save()
            painter.setPen(QtGui.QPen(QtGui.QColor("green")))
            r = QtCore.QRect(option.rect)
            r.adjust(0, 1, 0, -1)
            painter.drawLine(r.topLeft(), r.topRight())
            painter.drawLine(r.bottomLeft(), r.bottomRight())
            painter.restore()  
    def sizeHint(self, option, index):
        s = super(InventoryDelegate, self).sizeHint(option, index)
        s.setHeight(55)
        return s   
class Inventory_Items(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Inventory_Items, self).__init__(parent)
        self.inventory_dict = {
            "Shirt": {
                "Formal Blue shirt": ["10.10.1(Red)", "10.10.2(blue)"],
                "Discount:0%": ["300", "400"],
                "Unit:Quantity": ["10", "5"]
            },
            "Frocks": {
                "Partywear Frock": ["10.10.1(pink)", "10.10.1(black)"],
                "Discount:0%": ["900", "1200"],
                "Unit:Quantity": ["50", "30"]
            }

        }
        self.inventory_widget = QtGui.QTreeWidget(columnCount=7,
            expandsOnDoubleClick=False,
            indentation=0,
            focusPolicy=QtCore.Qt.NoFocus,
            iconSize=QtCore.QSize(40,60))

        self.inventory_widget.setColumnWidth(2, 350)
        self.inventory_widget.setStyleSheet("QWidget {background-color: black;color: white;}")
        header = self.inventory_widget.header()
        header.hide()
        delegate = InventoryDelegate(self.inventory_widget)
        self.inventory_widget.setItemDelegate(delegate)

        for key, value in self.inventory_dict.items():
            icon = QtGui.QIcon("./{}.png".format(key))
            it = QtGui.QTreeWidgetItem()
            self.inventory_widget.addTopLevelItem(it)
            it.setIcon(0, icon)
            k = list(value.keys())
            for i, t in enumerate([key] + k):
                it.setText(i+1, t)

            e = [[] for _ in value[k[0]]]
            for k, v in value.items():
                for i, val in enumerate(v):
                    e[i].append(val)
            for r in e:
                child = QtGui.QTreeWidgetItem()
                for i, e_ in enumerate(r):
                    child.setText(i+2, e_)
                    it.addChild(child)
        self.inventory_widget.expandAll()
        self.setCentralWidget(self.inventory_widget)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    settingobj= Inventory_Items()
    settingobj.showFullScreen()
    sys.exit(app.exec_())

A:

Assuming you want to change the color of the rows top-levels then you must overwrite the initStyleOption() method:
class InventoryDelegate(QtGui.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(InventoryDelegate, self).initStyleOption(option, index)
        if not index.parent().isValid():
            option.backgroundBrush = QtGui.QBrush(QtGui.QColor("gray"))

    def paint(self, painter, option, index):
        super(InventoryDelegate, self).paint(painter, option, index)
        if not index.parent().isValid():
            painter.save()
            painter.setPen(QtGui.QPen(QtGui.QColor("green")))
            r = QtCore.QRect(option.rect)
            r.adjust(0, 1, 0, -1)
            painter.drawLine(r.topLeft(), r.topRight())
            painter.drawLine(r.bottomLeft(), r.bottomRight())
            painter.restore()  

    def sizeHint(self, option, index):
        s = super(InventoryDelegate, self).sizeHint(option, index)
        s.setHeight(55)
        return s   

