Results 1 to 2 of 2

Thread: Editing a QTableWidgetItem and memory leaks

  1. #1
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Editing a QTableWidgetItem and memory leaks

    Good day,
    I'm trying to edit some QTableWidgetItem values and currently this is what I'm doing
    Qt Code:
    1. QTableWidgetItem *temp = new QTableWidgetItem("String 1");
    2. QTableWidgetItem *power = new QTableWidgetItem("String 2");
    3. QTableWidgetItem *dutyCycle = new QTableWidgetItem("String 3");
    4. QString mnemo = "String";
    5.  
    6. for(int i=0;i<29;i++)
    7. {
    8. QTableWidgetItem * head = ui->tLines_tableWidget->verticalHeaderItem(i);
    9. if (head->text()==mnemo)
    10. {
    11. ui->tLines_tableWidget->setItem(i,0,temp);
    12. ui->tLines_tableWidget->setItem(i,2,dutyCycle);
    13. ui->tLines_tableWidget->setItem(i,3,power);
    14. break;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    My question is, what happens with the item that i'm replacing? I create the new items with "new" but i never delete them, so do i have a memory leak here?
    If i'm losing memory here, how coud i avoid that?

    Thanks in advance,
    A.G.

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Editing a QTableWidgetItem and memory leaks

    http://doc.qt.nokia.com/4.7/qtablewidget.html#setItem

    Note that "The table takes ownership of the item." This means that the table is responsible for deleting the item at the appropriate time.

    If, for some reason, you want to get the old item back and manage it yourself use "takeItem".

  3. The following user says thank you to DanH for this useful post:

    AndresBarbaRoja (28th June 2011)

Similar Threads

  1. QT Memory Leaks
    By lukabratzi in forum Qt Programming
    Replies: 17
    Last Post: 28th February 2012, 20:01
  2. Memory Leaks
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2008, 16:26
  3. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 19:28
  4. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 08:00
  5. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 09:55

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
  •  
Qt is a trademark of The Qt Company.