Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. optimizing qtablewidget display using lambda functions
Forum Updated to NodeBB v4.3 + New Features

optimizing qtablewidget display using lambda functions

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 645 Views 1 Watching
  • 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.
  • L Offline
    L Offline
    Lelorrain
    wrote on last edited by
    #1

    Qtablewidget are not well known for their efficiency but I think here it is comming from my code.

    What I try to do: I try to change the background color of cells using RGBA data filled in 3D matrix, to display a picture.

    Here is the code I use:

    #RGB data creation using map function
    RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
            
    #Modify cell color
    for i in range(l):
        for j in range(h): #cell management, one by one...          
            self.tab.setItem(i,j,QTableWidgetItem())
            self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))
    

    Question improvement 1: is there a manner to "setItem" directly on every cells instead of doing it one by one

    Question improvement 2: is there a more effective way to set the background color than with two loops... I was expected something like this, but I have an "item(i,j)" issue:

    map(lambda R,G,B,A: self.tab.item(i,j).setBackground(R,G,B,A), RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)
    
    1. is there an interest by doing it with multithreading ? one cell by thread ?

    Thank you for answers

    jsulmJ 1 Reply Last reply
    0
    • L Lelorrain

      Qtablewidget are not well known for their efficiency but I think here it is comming from my code.

      What I try to do: I try to change the background color of cells using RGBA data filled in 3D matrix, to display a picture.

      Here is the code I use:

      #RGB data creation using map function
      RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
              
      #Modify cell color
      for i in range(l):
          for j in range(h): #cell management, one by one...          
              self.tab.setItem(i,j,QTableWidgetItem())
              self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))
      

      Question improvement 1: is there a manner to "setItem" directly on every cells instead of doing it one by one

      Question improvement 2: is there a more effective way to set the background color than with two loops... I was expected something like this, but I have an "item(i,j)" issue:

      map(lambda R,G,B,A: self.tab.item(i,j).setBackground(R,G,B,A), RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)
      
      1. is there an interest by doing it with multithreading ? one cell by thread ?

      Thank you for answers

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Lelorrain said in optimizing qtablewidget display using lambda functions:

      is there an interest by doing it with multithreading ? one cell by thread ?

      No!
      Especially not with one thread for each cell! In that case the overhead of creating threads would be way higher than the actual work.
      And it is not supported to change UI elements outside of main (UI) thread.

      "is there a manner to "setItem" directly on every cells instead of doing it one by one" - no.

      One small optimisation would be to first setup the item and then call setItem.

      You should also check style sheet for QTableWidget - maybe it is possible to define background for all cells.
      See: https://stackoverflow.com/questions/26162387/qtableview-qtablewidget-grid-stylesheet-grid-line-width
      https://doc.qt.io/qt-5/stylesheet-reference.html#qtableview-widget

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lelorrain
        wrote on last edited by Lelorrain
        #3

        I tried to write a lambda function to map the set item, but it seams it is not applying the set item. Any Idea why ?

        Original code:

        #RGB data creation using map function
        RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
                
        #Modify cell color
        for i in range(l):
            for j in range(h): #cell management, one by one...          
                self.tab.setItem(i,j,QTableWidgetItem())
                self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))
        

        My try:

        #RGB data creation using map function
        RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
                
        #Modify cell color
        map(lambda coor: self.tab.setItem(coor[0],coor[1],QTableWidgetItem()), np.meshgrid(range(l),range(h)))
        for i in range(l):
            for j in range(h): #cell management, one by one...          
                    self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))
        

        But when the for loop start, the setBackground is not working because item doesn't look to be created. There is something wrong between widget and lambda functions ?

        jsulmJ 1 Reply Last reply
        0
        • L Lelorrain

          I tried to write a lambda function to map the set item, but it seams it is not applying the set item. Any Idea why ?

          Original code:

          #RGB data creation using map function
          RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
                  
          #Modify cell color
          for i in range(l):
              for j in range(h): #cell management, one by one...          
                  self.tab.setItem(i,j,QTableWidgetItem())
                  self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))
          

          My try:

          #RGB data creation using map function
          RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
                  
          #Modify cell color
          map(lambda coor: self.tab.setItem(coor[0],coor[1],QTableWidgetItem()), np.meshgrid(range(l),range(h)))
          for i in range(l):
              for j in range(h): #cell management, one by one...          
                      self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))
          

          But when the for loop start, the setBackground is not working because item doesn't look to be created. There is something wrong between widget and lambda functions ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Lelorrain said in optimizing qtablewidget display using lambda functions:

          map(lambda coor: self.tab.setItem(coor[0],coor[1],QTableWidgetItem()), np.meshgrid(range(l),range(h)))
          for i in range(l):
          for j in range(h): #cell management, one by one...
          self.tab.item(i,j).setBackground(QBrush(QColor(RGBAs[i][j][0] * 255, RGBAs[i][j][1] * 255, RGBAs[i][j][2] * 255, RGBAs[i][j][3] * 255)))

          I doubt this approach will be any faster than the first one.
          Do you have a real performance issue or is this a case of premature optimisation? :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lelorrain
            wrote on last edited by Lelorrain
            #5

            yes, I have to load qtable widgets with this type of picture in background. number of lines and columns are > 100 on different tablewidget in parallel. and it is lagging at the opening... After that I try to select cells, and it also takes a lot of time :s

            Thank you for the time you take. In this development I don't whant user has a bad feeling just because it is lagging :s

            mrjjM 1 Reply Last reply
            0
            • L Lelorrain

              yes, I have to load qtable widgets with this type of picture in background. number of lines and columns are > 100 on different tablewidget in parallel. and it is lagging at the opening... After that I try to select cells, and it also takes a lot of time :s

              Thank you for the time you take. In this development I don't whant user has a bad feeling just because it is lagging :s

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Lelorrain

              Hi
              Are you sure the "lag" comes from the setBackground itself and
              not from loading data and the "mapping"
              RGBAs = list(map(lambda x: cmap(norm(x)), data[:, :]))
              and the construction of the QTableWidgetItem's ?

              1 Reply Last reply
              0

              • Login

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