Results 1 to 4 of 4

Thread: QStandardItemModel with QStandardItem usage

  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QStandardItemModel with QStandardItem usage

    I have a function that I call from my main form. I have a tableView on my main form.

    First I create a model like this :
    QStandardItemModel *model = new QStandardItemModel((frame->NM * frame->NLC), 6, this);

    Then I populate it like this. This is done in a loop :
    QStandardItem *stdd = new QStandardItem(QString::number(frame->member[i]->ResultantForce[ind]));
    stdd->setTextAlignment(Qt::AlignHCenter);
    model->setItem(m, n, stdd);


    My question is when are the hundreds of the QStandardItem *stdd I created in a loop are deleted from memory? Do I have to delete them manually by using delete stdd or they are deleted automatically when the model is deleted? What about the model itself? When is it removed from memory.

    Thank you in advance...

  2. #2
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStandardItemModel with QStandardItem usage

    I would like to clarify my question:
    1) Does model->clear() remove all the QStandardItem objects from memory? If not, then how do I remove them?
    2) Do I have to remove the model itself manually or it will be removed automatically when the parent is destroyed?

    The way memory is used and then reclaimed by these objects is not explained clearly in the manuals. I use QT Creator on windows 7.0 32 bit.

    Thanks...

  3. #3
    Join Date
    Oct 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel with QStandardItem usage

    I would like to add an addtional question to this post...
    When you add user defined data to a QStandardItem, is that cleaned up when model->clear() is called?
    Some experiments that we are doing would indicate there is a memory leak. We update the model by clearing it, allocating new QStandardItems and add them to the model... The memory consumption increases over time.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QStandardItemModel with QStandardItem usage

    Quote Originally Posted by jove04 View Post
    My question is when are the hundreds of the QStandardItem *stdd I created in a loop are deleted from memory? Do I have to delete them manually by using delete stdd or they are deleted automatically when the model is deleted? What about the model itself? When is it removed from memory.
    From the docs for QStandardItemModel::setItem()
    The model takes ownership of the item.
    The items will definitely be destroyed when the model itself is destroyed. They probably will be destroyed earlier if you use clear() on the model but I have not verified that.

    The model itself, in this situation, will definitely be destroyed when the the object "this" is destroyed unless you choose to do it yourself earlier. When you create a QObject with a parent, or set the parent directly or indirectly after creation, then the parent will delete it as part of parent's destruction. See the the article "Object Trees and Object Ownership" in Assistant and the detailed description section of the QObject docs.


    Edit: Just did the experiment, clear() does trigger item delete
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MyItem: public QStandardItem
    5. {
    6. public:
    7. MyItem(const QString & text): QStandardItem(text) {
    8. qDebug() << "Item created" << this;
    9. }
    10. ~MyItem() {
    11. qDebug() << "Item destroyed" << this;
    12. }
    13. };
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QApplication app(argc, argv);
    18.  
    19. QStandardItemModel model(4, 4);
    20. for (int row = 0; row < 4; ++row) {
    21. for (int column = 0; column < 4; ++column) {
    22. QStandardItem *item =
    23. new MyItem(QString("row %0, column %1").arg(row).arg(column));
    24. model.setItem(row, column, item);
    25. }
    26. }
    27.  
    28. qDebug() << "Finished making model";
    29. model.clear();
    30. qDebug() << "Model cleared";
    31. }
    To copy to clipboard, switch view to plain text mode 


    Added after 5 minutes:


    mnh1959: If the "user defined data" is a value type then sure. If it is a pointer then the space for the pointer is freed but object pointed to is not. You have to manage the target of the pointer yourself (in the destructor of a QStandardItem derivative).
    Last edited by ChrisW67; 29th October 2010 at 23:26.

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

    ajo (28th May 2013)

Similar Threads

  1. HTML and QStandardItem ?
    By alexandernst in forum Newbie
    Replies: 18
    Last Post: 4th August 2009, 12:07
  2. UTF8 and QStandardItem?
    By alexandernst in forum Newbie
    Replies: 17
    Last Post: 26th July 2009, 18:29
  3. QStandardItem question.
    By alexandernst in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2009, 01:01
  4. Subclassing QStandardItem
    By aLiNuSh in forum Newbie
    Replies: 4
    Last Post: 5th April 2007, 20:00
  5. QStandardItem::type() question
    By xgoan in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2007, 14:39

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.