我正在运行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_())发布于 2020-03-02 09:54:05
您可以在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);
}https://stackoverflow.com/questions/60455316
复制相似问题