QTreeView'show-decoration-selected:0;'没有效果

问题描述 投票:0回答:1

我正在运行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_())
python pyqt pyqt4
1个回答
0
投票

您可以在QTreeView::item上设置选择颜色和背景色,并为QTreeView::branch设置背景。但是,当您覆盖分支背景时,将需要包括用于分支箭头的图像。这是Qt提供的。

分支关闭-branch closed

分支开放-enter image description here

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.