PDA

View Full Version : Removing a QWidget object from a QTableWidget cell



mclark
22nd January 2008, 19:23
My question involves the removal of QWidgets placed in QTableWidget cells. I have cells that contain text, QComboBox and QPushButton objects which I initialize like this:

// Initialize table cells. Create a item for COL_2 to aid in sorting.
setItem( m_nCurrRow, COL_1, new QTableWidgetItem( "" ) );
setItem( m_nCurrRow, COL_2, new QTableWidgetItem( "" ) );
setCellWidget( m_nCurrRow, COL_2, new QComboBox( this ) );
setCellWidget( m_nCurrRow, COL_3, new QPushButton( tr("Edit"), this ) );

My question is, do I need to explicitly remove the non-QTableWidgetItem ojbects to prevent memory leaks or Qt side-effects?


QWidget* pWidget;
if ( (pWidget = cellWidget( nRow, COL_2 )) != NULL )
delete pWidget;

if ( (pWidget = cellWidget( nRow, COL_3 )) != NULL )
delete pWidget;
This not just an issue for shutdown. The app is able to remove one or more (all) table rows. My concern is that I am doing unnecessary processing that may cause side-effects when Qt is destroying its objects.

wysota
22nd January 2008, 20:13
Index widgets will be deleted by the view when they are not needed anymore as the view is their parent.

mclark
22nd January 2008, 20:29
Thanks for the reply wysota.

Are you saying that when a row in a QTableWidget is removed (by removeRow( 1 ) for example) that all widgets in the cells will be automically deleted?

wysota
22nd January 2008, 22:52
Yes, that's correct.

mclark
13th March 2008, 14:47
A follow-up question involves subclassing QTableWidgetItem. From some comments in the forums it appears that when constructing a QTableWidgetItem and specifying a type larger than QTableWidgetItem::UserType, a QTableWidget will be able to delete the subclass when its cell is removed.

Is it true that when the row is removed, all the memory allocated for the PortModeCell class in the following example is also automatically deleted.


class PortModeCell : public QTableWidgetItem
{
public:
PortModeCell( int nColumn, QWidget* pParent = 0 ) : QTableWidgetItem( "", QTableWidgetItem::UserType + 1 )
{
m_nColumn = nColumn;
m_parent = reinterpret_cast<DMXTable*>( pParent );
m_bUpdateDevice = false;
}

// Operator for sorting non-alpha table columns
bool operator<( const QTableWidgetItem& item ) const;

// Explicitly control the changing of the comboBox index
void setIndex( int nIndex, bool bUpdateDevice );
void setCurrentIndex( int nIndex );
int currentIndex() { return m_nIndex; }
void setEnabled( bool bEnabled );
void setCellTextColor( int nIndex );
void indexChanged( int nIndex );

public:
int m_nColumn;
int m_nIndex;
DMXTable* m_parent;
bool m_bUpdateDevice;
void* m_pData; // give each cell a data pointer
};

. . .

// In a QTableWidget class we have...

PortModeCell* pCell = new PortModeCell( 3, this );
if ( pCell == NULL )
return; // Log an error

setItem( nRow, nCol, pCell );

wysota
13th March 2008, 15:19
From what I see the item doesn't contain any data it owns, so all memory will be freed. Only if the object contained some additional data (i.e. allocated in the constructor) you would have to free it yourself in the destructor.