PDA

View Full Version : Populating a QtableWidget using a list of custom widgets



gerocampo
4th August 2010, 11:16
Good morning

I have a problem with population of a qtablewidget using custom widgets stored in a list.

the code for create the widgets is:


xdate=[None]*3200
n_projects=100
for i in range(0,n_projects):
label="Hello"
xdate[i]=QtGui.QLabel()
xdate[i].setText(label)
self.ui.TW1.setCellWidget(13,i,xdate[i])

Then I could see the custom widgets without problem. Then in the program I want to change the text of the qlabels and reinserting using the following code:


for i in range(0,n_projects):
xdate[i].setText("Option"+str(i))
self.ui.TW1.setCellWidget(13,i,xdate[i])

and now the widgets doesn't appear in the table.

Why is happen this?

Many thanks for your help

German

Lykurg
4th August 2010, 11:28
Adding a lot of custom widget to a table widget is not the best solution. So better check if you could use a model or at least subclass QTableWidgetItem and alter the paint method to fit your needs. (or use a custom delegate);

the problem why your code is not working, is that setCellWidget replaces the old widget, so your first widget is deleted and then Qt tries to set the new one which actually was deleted. So to alter your text use something likeself.ui.TW1.cellWidget(13, i)->setText("Option"+str(i))