Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidgetItem setter functions in PyQt5 are very slow?
QtWS25 Last Chance

QListWidgetItem setter functions in PyQt5 are very slow?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MiBr
    wrote on 6 Jul 2017, 18:52 last edited by
    #1

    Hello, I just want to preface this by saying this is my first time posting, any feedback will be appreciated.

    This is a simple representation of the problem, I am using a QTableWidget and the variable cell is a QTableWidgetItem. I am trying to change the background color of about 400 QTableWidgetItems in a QTableWidget, I am using setBackground but have also tried setData and setFont with the same results.

    color = QtGui.QColor()
    column = 3
    for row in rowIndexList:
        cell = table.item(row,column)
        color.setNamedColor('#00ff00')
        start = time.time()
        cell.setBackground(color)
        end = time.time()
        print('time to set background' + str(end-start))
    

    The print statement from this code will print about 0.13-0.15 seconds for every iteration, and this loop iterates 400 times. Is there a reason the setBackground function for the QTableWidgetItem is so slow? Do I need to use a QTableView with delegates because of this?

    Here is some output I set up for my program
    0_1499366718074_output.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 6 Jul 2017, 19:11 last edited by
      #2

      Hi and welcome to devnet,

      Part of this is likely due to the fact that you'll trigger a repaint for the whole widget each time you change an item background colour.

      You could disable the updates from your view. Update your model and then enable the updates again.

      Note that there's no need to set the colour of your color variable for each index. You can do it before your for loop.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MiBr
        wrote on 6 Jul 2017, 20:12 last edited by
        #3

        I see, thanks for the reply; you are right about the color variable. I wrote the code snippet quickly, in the actual function the variable possibly changes with every loop and is not hard coded to '#00ff00'.

        I will attempt to replace my qtablewidget with a qtableview and custom model, then I will respond back with the results.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MiBr
          wrote on 6 Jul 2017, 21:07 last edited by MiBr 7 Jun 2017, 22:56
          #4

          I modified the code to be

          class CustomTableView(QTableView):
              def __init__(self,parent=None):
                  super().__init__(parent)
          
          class CustomStandardModel(QtGui.QStandardItemModel):
              def __init__(self,parent=None):
                  super().__init__(parent)
          
          class CustomStandardItem(QtGui.QStandardItem):
              def __init__(self,parent=None):
                  super().__init__(parent)
          
          table = CustomTableView()
          model = CustomStandardModel()
          table.setModel(model)
          
          /...
          #fill the model with CustomStandardItem objects
          .../
          
          color = QtGui.QColor()
          color.setNamedColor('#00ff00')
          column = 3
          for row in rowIndexList:
              cell = model.item(row,column)
              start = time.time()
              cell.setBackground(color)
              end = time.time()
              print('time to set background' + str(end-start))
          

          And this is significantly faster than the qtablewidget.

          output:
          0_1499375234983_Screen Shot 2017-07-06 at 2.06.59 PM.png

          EDIT:
          This loop for setting columns to autoresize was causing the initial problem of setBackground taking so long, with it commented out the QTableWidget is as fast as the QTableView. This loop runs once after the table is loaded with values, however every time setBackground is called it is affected.

          for col in range(self.columnCount()):   
               self.horizontalHeader().setSectionResizeMode(col,QHeaderView.ResizeToContents)
          
          1 Reply Last reply
          0

          1/4

          6 Jul 2017, 18:52

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved