1

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.

Thomasedv
  • 382
  • 4
  • 22
  • What you want to remove are the arrows? – eyllanesc Jul 03 '17 at 16:29
  • try with: `self.tree.setStyleSheet( "QTreeWidget::branch{border-image: url(none.png);}")` – eyllanesc Jul 03 '17 at 16:33
  • @eyllanesc That does only hide the arrows, not remove their functionality too. unfortunately. I also tried: using "border: none" in it, with the same result. – Thomasedv Jul 03 '17 at 16:58
  • This conceals it for all items and in practical form is the same, or do you want to eliminate that space remaining? – eyllanesc Jul 03 '17 at 17:01
  • @eyllanesc I can still press the blank spaces and have the tree collapse.(on second branch, since there will be an indentation there.) That doesn't happen with the second point in the question, but also removes any future children down the chain it seems. – Thomasedv Jul 03 '17 at 17:15
  • So you do not want it to collapse when you click? – eyllanesc Jul 03 '17 at 17:16
  • @eyllanesc Yes, i don't want to collapse it on click,. (Though, if possible, i want to do it programatically, which should be much easier.) – Thomasedv Jul 03 '17 at 17:33

1 Answers1

1

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)
Adam Sirrelle
  • 357
  • 8
  • 18
  • 1
    It's been a long time since I touched upon this, but i'll mark this as the solution as it is close to what I ended up doing. However I set isRootDecorated instead of setItemsExapandable. I do not know the difference, but i'm still able to programmatically expand a branch when it's been selected using my method. – Thomasedv Apr 29 '21 at 14:58