Issue
I found this code snippet of code on SO:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.tree = QtGui.QTreeWidget(self)
self.tree.setHeaderHidden(True)
for index in range(2):
parent = self.addItem(self.tree, 'Item%d' % index)
for color in 'Red Green Blue'.split():
subitem = self.addItem(parent, color)
for letter in 'ABC':
self.addItem(subitem, letter, True, False)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.tree)
self.tree.itemChanged.connect(self.handleItemChanged)
def addItem(self, parent, text, checkable=False, expanded=True):
item = QtGui.QTreeWidgetItem(parent, [text])
if checkable:
item.setCheckState(0, QtCore.Qt.Unchecked)
else:
item.setFlags(
item.flags() & ~QtCore.Qt.ItemIsUserCheckable)
item.setExpanded(expanded)
return item
def handleItemChanged(self, item, column):
if item.flags() & QtCore.Qt.ItemIsUserCheckable:
path = self.getTreePath(item)
if item.checkState(0) == QtCore.Qt.Checked:
print('%s: Checked' % path)
else:
print('%s: UnChecked' % path)
def getTreePath(self, item):
path = []
while item is not None:
path.append(str(item.text(0)))
item = item.parent()
return '/'.join(reversed(path))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 250, 450)
window.show()
I wonder, how can i hide the decorators from the result, on all of the items?
I know i can hide with setstylesheet doesn't actually removed the arrows, just hide them, which is counterproductive if you accidentally hide them.
item.setChildPolicy(QTreeWidgetItem.DontShowIndicator)
either removes the children, or permanently closes them, because the subitems(children of item) all disappear once i do that, and can't do anything with... Tried to expand too, does't work for me.
Actually in PyQt5, so the answer doesn't need to be in PyQt4.
Solution
I got a potential solution from here: How do you disable expansion in QTreeWidget/QTreeView?
Using setStyleSheet, as suggested by eyllanesc, to visibly hide the arrow decorator:
self.tree.setStyleSheet( "QTreeWidget::branch{border-image: url(none.png);}")
and then setting:
self.tree.setItemsExpandable(False)
You can successfully hide and disable the arrow decorators.
Personally, I have used this method when using a QPushButton to control the expanding of the QTreeWidgetItems.
example code
def __init__(self):
self.button = QtWidgets.QPushButton(text="toggle tree item")
self.button.setCheckable = True
self.button.toggled.connect(self.button_clicked)
def button_clicked(self, toggled):
self.tree_widget_item.setExpanded(toggled)
Answered By - Adam Sirrelle
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.