Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Does QTableWidget::setItem() automatically delete the previous object when it is called for the second time?

Does QTableWidget::setItem() automatically delete the previous object when it is called for the second time?

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetqt6
2 Posts 2 Posters 591 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zhmou
    wrote on 18 Jul 2022, 08:03 last edited by
    #1

    When I read setCellWidget() function, the doc says If cell widget A is replaced with cell widget B, cell widget A will be deleted. and gives out a code snippet example:

    setCellWidget(row, column, new QLineEdit);
    ...
    //Previously created QLineEdit object will be deleted.
    setCellWidget(row, column, new QTextEdit);
    

    Now I am wondering does setItem() works like setCellWidget() ? For example:

    
    setItem(row, column, new QTableWidgetItem);
    ...
    
    //Do I need to manually delete the previous QTableWidgetItem object, or free up the memory occupied by the pointer in other words?
    setItem(row, column, new QTableWidgetItem);
    
    J 1 Reply Last reply 18 Jul 2022, 08:09
    0
    • Z Zhmou
      18 Jul 2022, 08:03

      When I read setCellWidget() function, the doc says If cell widget A is replaced with cell widget B, cell widget A will be deleted. and gives out a code snippet example:

      setCellWidget(row, column, new QLineEdit);
      ...
      //Previously created QLineEdit object will be deleted.
      setCellWidget(row, column, new QTextEdit);
      

      Now I am wondering does setItem() works like setCellWidget() ? For example:

      
      setItem(row, column, new QTableWidgetItem);
      ...
      
      //Do I need to manually delete the previous QTableWidgetItem object, or free up the memory occupied by the pointer in other words?
      setItem(row, column, new QTableWidgetItem);
      
      J Offline
      J Offline
      JonB
      wrote on 18 Jul 2022, 08:09 last edited by
      #2

      @Zhmou
      Because https://doc.qt.io/qt-6/qtablewidget.html#setItem says:

      The table takes ownership of the item.

      that means it owns it, and will dispose it if you replace it.

      1 Reply Last reply
      1

      2/2

      18 Jul 2022, 08:09

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved