1

I'm struggling with finding a solution to clear() all QTreeWidget items. I don't want to remove each item separately but want to do it in a single shot as I might have tens of thousands of items to clear. So I have a function clear_tree which suppose to clear the tree and display "Wait..." in a tree until the next function long process will finish walking through the massive folder and display thousands of files.

import sys
import time
from PyQt5.QtWidgets import QApplication, QTreeWidgetItem,  QPushButton, QTreeWidget, QVBoxLayout, QWidget
        
        class AppDemo(QWidget):
            def __init__(self):
                super().__init__()
                self.layout = QVBoxLayout()
                self.resize(500,700)
                self.tree = QTreeWidget()
                self.tree.setHeaderLabels((['Name', 'Date', 'Size', 'Path']))
                self.tree.size()
        
                self.button = QPushButton("Button", self)
                self.layout.addWidget(self.button)
                self.layout.addWidget(self.tree)
                
                self.button.clicked.connect(self.clear_tree)
        
                self.setLayout(self.layout)
                self.show()
        
            def clear_tree(self):
                
                # Displaying temporary message while 'long process' i in progress
                self.tree.clear()
                items = [QTreeWidgetItem(["Wait...................."])]
                self.tree.insertTopLevelItems(0, items)
                self.long_proces()
            
            def long_proces(self):
                items = []
                # Simulating of 'long process'
                for i in range(0, 5):
                    print(i)
                    time.sleep(.3)
        
                # Displaying items resulted from 'long process'
                for i in (["Item 1"], ["Item 2"], ["Item 3"], ["Item 4"], ["Item 5"], ["Item 6"]):
                    items.append(QTreeWidgetItem(i))
                self.tree.clear()
                self.tree.insertTopLevelItems(0, items)
        
        # initialize The App
        app = QApplication(sys.argv)
        demo = AppDemo()
        app.exec_()

After a whole day of investigation, I believe that the problem is caused by Python not returning to the AppDemo class which is responsible for the change of displayed items in the Tree because the two functions follow each other consecutively.

I was trying to break this chain of two functions with many methods but the best case result was that the button had to be hit two times, first when initiating the whole operation and second when the first function terminated and the second function had to be initiated. Has anybody got an idea of how to overcome this problem?

Zwornik
  • 31
  • 5

0 Answers0