1

我有一个带有彩色背景的 QTreeView,但我还需要根颜色。我不能这样做。有人可以帮助我吗?这是代码..

谢谢

从 PyQt4 导入 QtGui,QtCore

HORIZONTAL_HEADERS = ("姓氏", "名字")

类 person_class(object): def init (self, sname, fname, isMale): self.sname = sname self.fname = fname self.isMale = isMale

类 TreeItem(object): def init (self, person, header, parentItem): self.person = person self.parentItem = parentItem self.header = header self.childItems = []

def appendChild(self, item):
    self.childItems.append(item)

def child(self, row):
    return self.childItems[row]

def childCount(self):
    return len(self.childItems)

def columnCount(self):
    return 2

def data(self, column):
    if self.person == None:
        if column == 0:
            return QtCore.QVariant(self.header)
        if column == 1:
            return QtCore.QVariant("")                
    else:
        if column == 0:
            return QtCore.QVariant(self.person.sname)
        if column == 1:
            return QtCore.QVariant(self.person.fname)
    return QtCore.QVariant()

def parent(self):
    return self.parentItem

def row(self):
    if self.parentItem:
        return self.parentItem.childItems.index(self)
    return 0

类 treeModel(QtCore.QAbstractItemModel): def init (self, parent=None): super(treeModel, self)。init (parent) self.people = [] for fname, sname, isMale in (("John","Brown", 1), ("Fred", "Bloggs", 1), ("Sue", "Smith" , 0)): person = person_class(sname, fname, isMale) self.people.append(person)

    self.rootItem = TreeItem(None, "ALL", None)
    self.parents = {0 : self.rootItem}
    self.setupModelData()

def columnCount(self, parent=None):
    if parent and parent.isValid():
        return parent.internalPointer().columnCount()
    else:
        return len(HORIZONTAL_HEADERS)

def data(self, index, role):
    if not index.isValid():
        return QtCore.QVariant()

    item = index.internalPointer()
    if role == QtCore.Qt.DisplayRole:
        return item.data(index.column())
    if role == QtCore.Qt.UserRole:
        if item:
            return item.person
    if role == QtCore.Qt.BackgroundRole :
        return QtGui.QColor('pink') 


    return QtCore.QVariant()

#def headerData(self, column, orientation, role):
    #if (orientation == QtCore.Qt.Horizontal and
    #role == QtCore.Qt.DisplayRole):
        #try:
            #return QtCore.QVariant(HORIZONTAL_HEADERS[column])
        #except IndexError:
            #pass

    #return QtCore.QVariant()

def index(self, row, column, parent):
    if not self.hasIndex(row, column, parent):
        return QtCore.QModelIndex()

    if not parent.isValid():
        parentItem = self.rootItem
    else:
        parentItem = parent.internalPointer()

    childItem = parentItem.child(row)
    if childItem:
        return self.createIndex(row, column, childItem)
    else:
        return QtCore.QModelIndex()

def parent(self, index):
    if not index.isValid():
        return QtCore.QModelIndex()

    childItem = index.internalPointer()
    if not childItem:
        return QtCore.QModelIndex()

    parentItem = childItem.parent()

    if parentItem == self.rootItem:
        return QtCore.QModelIndex()

    return self.createIndex(parentItem.row(), 0, parentItem)

def rowCount(self, parent=QtCore.QModelIndex()):
    if parent.column() > 0:
        return 0
    if not parent.isValid():
        p_Item = self.rootItem
    else:
        p_Item = parent.internalPointer()
    return p_Item.childCount()

def setupModelData(self):
    for person in self.people:
        if person.isMale:
            sex = "MALES"
        else:
            sex = "FEMALES"

        if not self.parents.has_key(sex):                
            newparent = TreeItem(None, sex, self.rootItem)
            self.rootItem.appendChild(newparent)

            self.parents[sex] = newparent

        parentItem = self.parents[sex]
        newItem = TreeItem(person, "", parentItem)
        parentItem.appendChild(newItem)

如果名称== “主要”:

app = QtGui.QApplication([])
model = treeModel()
dialog = QtGui.QDialog()
dialog.setMinimumSize(300,150)
layout = QtGui.QVBoxLayout(dialog)

tv = QtGui.QTreeView(dialog)
tv.setModel(model)
layout.addWidget(tv)

dialog.exec_()
app.closeAllWindows()
4

0 回答 0