pyqt4 Table Widget deleting c/c++ object

David Boddie david at boddie.org.uk
Tue Oct 19 17:29:52 EDT 2010


On Monday 18 October 2010 23:26, Andrew wrote:

> I have two issues dealing with the table widget, though they may be
> interconnected. I'm not sure. Both delete the cell widgets off of my
> table but leave the rows, and then when I have the table update, it
> complains the c++ object has been deleted.
> 
> # self.tableData.setCellWidget(rowCount, 0, trackItem)
> # RuntimeError: underlying C/C++ object has been deleted

This is because you pass your widgets to this method and later ask
the table to clear the contents of the table. When it does so, it
deletes the underlying widgets, leaving only Python wrappers.

The documentation mentions that the table takes ownership of the
widget:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtablewidget.html#setCellWidget

> I have a list of a custom widget class that keeps track of several
> directories. Each class gets put into a row on the table. I have a set
> of radio buttons that will show me different views of the data: All,
> New, Done, Errors, Warnings.
> When I switch views, I clear my table, set the row count to 0, and
> then add back in only the widgets I want to see.
> 
> Error 1:
> I load in the data initially, and then repopulate the table, and then
> re-size the window, instantly all listed rows are cleared, but the
> rows stay. This only happened on the diagonal re-size (bottom left
> corner); not the up and down, or side to side re-size. Attempting to
> repopulate the table, resulted in: underlying C/C++ object has been
> deleted. Though it will put in the correct number of rows required.
> 
> Everything worked fine as long as I did not re-size the window.

This may only be a symptom of the behaviour and not a guarantee that
the code was working correctly up until the point when the resize
occurred.

> Error 2:
> I load in the data initially, and then repopulate the table, the table
> clears and then nothing happens. No error messages or even visible
> rows. After several more repopulates it with either crash or tell me:
> underlying C/C++ object has been deleted.
> 
> I had error 1 two days ago, then without changing the code, I now get
> error 2. I do not have to re-size the window for it to break now.

I recommend that you create a list of non-widget data structures in
your parsePath() method and create widgets on the fly in your addToTable()
method. If you need to retain information when you repopulate the table
then update the data structures that correspond to the widgets just before
you clear the table.

David



More information about the Python-list mailing list