gpt4 book ai didi

python - QtreeWidget样式表: change selected item style

转载 作者:行者123 更新时间:2023-12-01 05:22:11 25 4
gpt4 key购买 nike

我正在尝试更改 QtTreeWidget 中当前所选项目的样式。不幸的是,样式仅应用于顶层项目,而不应用于顶层的子项。有没有人遇到过这个问题并有解决方案?

样式表在 QtDesigner 中工作正常。我不知道为什么它在我的代码中不起作用。我正在努力将代码提取到示例中。

<小时/>

样式表:

QTreeWidget::item:selected { border-color:green; 
border-style:outset; border-width:2px; color:black; }

QTreeWidget::branch:has-children:!has-siblings:closed,
QTreeWidget::branch:closed:has-children:has-siblings {
border-image: none;
image: none;
}

QTreeWidget::branch:open:has-children:!has-siblings,
QTreeWidget::branch:open:has-children:has-siblings {
border-image: none;
image: none;
}
<小时/>

完整示例代码

import sys
from PyQt4 import QtGui, QtCore

class ConditionTree(QtGui.QTreeWidgetItem):
def __init__(self, parent, name):
QtGui.QTreeWidgetItem.__init__(self, parent)
self.tree = parent

self.setText(0, name)

self.button = QtGui.QPushButton(name)
self.button.pressed.connect(self.buttonPress)

def buttonPress(self):
self.setExpanded(not self.isExpanded())
self.tree.setCurrentItem(self)

class mainWindow(QtGui.QMainWindow):
'''
Main window class handeling all gui interactions
'''
def __init__(self, app):
QtGui.QMainWindow.__init__(self)


self.tree = QtGui.QTreeWidget(self)

self.tree.setIndentation(0)
self.tree.setHeaderHidden(True)

self.tree.setStyleSheet('''
QTreeWidget::item:selected { border-color:green;
border-style:outset; border-width:2px; color:black; }
QTreeWidget::branch:has-children:!has-siblings:closed,
QTreeWidget::branch:closed:has-children:has-siblings {
border-image: none;
image: none;
}

QTreeWidget::branch:open:has-children:!has-siblings,
QTreeWidget::branch:open:has-children:has-siblings {
border-image: none;
image: none;
}

''')

self.topLevel = ConditionTree(self.tree, 'toplevel')
self.tree.setItemWidget(self.topLevel, 0, self.topLevel.button)


for i in range(5):
self.addNewItem(self.topLevel, str(i))

self.tree.resize(self.tree.sizeHint())

def addNewItem(self, toplevel, name):
newItem = QtGui.QTreeWidgetItem()
newItem.setText(0, name)
newItem.setFlags(QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsEnabled)

toplevel.addChild(newItem)


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)

gui = mainWindow(app)
gui.show()
app.exec_()

app.deleteLater()
sys.exit()
<小时/>

enter image description here

最佳答案

子项目不可选择。由于您无法选择它们,因此它们不会被设置样式。在 addNewItem 方法中修改这一行:

newItem.setFlags(QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsEnabled)

newItem.setFlags(QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsSelectable)

关于python - QtreeWidget样式表: change selected item style,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com