我正在运行PyQt4
,并尝试在所选项目上制作没有装饰颜色的QTreeWidget
。但是,以下代码无效,并且QTreeWidgetItem
左侧的颜色在选择时仍会上色。
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.mainLayout = QtGui.QVBoxLayout(self)
self.setLayout(self.mainLayout)
self.tree = QtGui.QTreeWidget()
self.mainLayout.addWidget(self.tree)
for i in range(20):
item = QtGui.QTreeWidgetItem(self.tree)
item.setText(0, 'This Is Item #{}'.format(i))
for i in range(4):
child_item = QtGui.QTreeWidgetItem(item)
self.tree.setStyleSheet('''
QTreeView{
show-decoration-selected: 0;
}
''')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
您可以在QTreeView::item
上设置选择颜色和背景色,并为QTreeView::branch
设置背景。但是,当您覆盖分支背景时,将需要包括用于分支箭头的图像。这是Qt提供的。
QTreeView::item {
selection-color: #000;
selection-background-color: rgba(0, 0, 0, 0%);
}
QTreeView::branch:selected:closed {
background: white;
image: url(branch-closed.png);
}
QTreeView::branch:selected:open {
background: white;
image: url(branch-open.png);
}