Results 1 to 6 of 6

Thread: Removing a QWidget object from a QTableWidget cell

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Removing a QWidget object from a QTableWidget cell

    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:
    Qt Code:
    1. // Initialize table cells. Create a item for COL_2 to aid in sorting.
    2. setItem( m_nCurrRow, COL_1, new QTableWidgetItem( "" ) );
    3. setItem( m_nCurrRow, COL_2, new QTableWidgetItem( "" ) );
    4. setCellWidget( m_nCurrRow, COL_2, new QComboBox( this ) );
    5. setCellWidget( m_nCurrRow, COL_3, new QPushButton( tr("Edit"), this ) );
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QWidget* pWidget;
    2. if ( (pWidget = cellWidget( nRow, COL_2 )) != NULL )
    3. delete pWidget;
    4.  
    5. if ( (pWidget = cellWidget( nRow, COL_3 )) != NULL )
    6. delete pWidget;
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Removing a QWidget object from a QTableWidget cell

    Index widgets will be deleted by the view when they are not needed anymore as the view is their parent.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Removing a QWidget object from a QTableWidget cell

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Removing a QWidget object from a QTableWidget cell

    Yes, that's correct.

  5. The following user says thank you to wysota for this useful post:

    mclark (23rd January 2008)

  6. #5
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Removing a QWidget object from a QTableWidget cell

    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.

    Qt Code:
    1. class PortModeCell : public QTableWidgetItem
    2. {
    3. public:
    4. PortModeCell( int nColumn, QWidget* pParent = 0 ) : QTableWidgetItem( "", QTableWidgetItem::UserType + 1 )
    5. {
    6. m_nColumn = nColumn;
    7. m_parent = reinterpret_cast<DMXTable*>( pParent );
    8. m_bUpdateDevice = false;
    9. }
    10.  
    11. // Operator for sorting non-alpha table columns
    12. bool operator<( const QTableWidgetItem& item ) const;
    13.  
    14. // Explicitly control the changing of the comboBox index
    15. void setIndex( int nIndex, bool bUpdateDevice );
    16. void setCurrentIndex( int nIndex );
    17. int currentIndex() { return m_nIndex; }
    18. void setEnabled( bool bEnabled );
    19. void setCellTextColor( int nIndex );
    20. void indexChanged( int nIndex );
    21.  
    22. public:
    23. int m_nColumn;
    24. int m_nIndex;
    25. DMXTable* m_parent;
    26. bool m_bUpdateDevice;
    27. void* m_pData; // give each cell a data pointer
    28. };
    29.  
    30. . . .
    31.  
    32. // In a QTableWidget class we have...
    33.  
    34. PortModeCell* pCell = new PortModeCell( 3, this );
    35. if ( pCell == NULL )
    36. return; // Log an error
    37.  
    38. setItem( nRow, nCol, pCell );
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Removing a QWidget object from a QTableWidget cell

    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.

  8. The following user says thank you to wysota for this useful post:

    mclark (13th March 2008)

Similar Threads

  1. Word wrapping in a QTableWidget cell
    By jcooperddtd in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 03:57
  2. Selection of cell with QWidget in QTableWidget
    By Tamara in forum Qt Programming
    Replies: 7
    Last Post: 17th February 2007, 14:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.