Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2481

slow QTreeWidget.clear

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 6.3.1, 6.5.3
    • PySide
    • None
    • Windows 10x64
      CPython 3.10 x64
      PySide6 6.3.1 and 6.5.3
    • Windows

    Description

      There seems to be a quadratic behavior to the clear method

       

      import sys
      from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTreeWidget, QTreeWidgetItem, QPushButton, QSpinBox
      from PySide6.QtGui import QPalette, QColor
      
      
      class MainWindow(QMainWindow):
      
          def __init__(self):
              super(MainWindow, self).__init__()
      
              self.setWindowTitle("My App")
      
              layout = QVBoxLayout()
      
              self.t = QTreeWidget()
      
              b = QPushButton('clear tree')
              b.clicked.connect(self.clr)
      
              self.n = n = QSpinBox()
              n.setMaximum(1000000)
              n.setValue(1000)
      
              a = QPushButton('Add items')
              a.clicked.connect(self.add)
      
              layout.addWidget(self.t)
              layout.addWidget(n)
              layout.addWidget(a)
              layout.addWidget(b)
      
              widget = QWidget()
              widget.setLayout(layout)
              self.setCentralWidget(widget)
      
          def add(self):
              count = self.n.value()
              if count:
                  items = [QTreeWidgetItem([]) for i in range(count)]
      
                  self.t.addTopLevelItems(items)
      
          def clr(self):
              from time import perf_counter
              count = self.t.topLevelItemCount()
              t1 = perf_counter()
              self.t.clear()
              t2 = perf_counter()
              print(f'{count=} took {t2-t1}')
      
      
      app = QApplication(sys.argv)
      
      window = MainWindow()
      window.show()
      
      app.exec_() 

       Running this with 1000, 10_000 and 100_000 items shows this print 

       

      count=1000 took 0.001806599997507874
      count=10000 took 0.02987070000017411
      count=100000 took 1.43678160000126 

      If the tree has multiple levels then it behaves even worse

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              crmaurei Cristian Maureira-Fredes
              danielhrisca Daniel Hrisca
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes