optimizing qtablewidget display using lambda functions
-
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)
- is there an interest by doing it with multithreading ? one cell by thread ?
Thank you for answers
-
@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 -
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 ?
-
@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? :-) -
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
-
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 ?