Results 1 to 15 of 15

Thread: Why object on the heap for QStandardItemModel?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Why object on the heap for QStandardItemModel?

    Hi all.
    I'm trying to populate a QTableView with some data. Here is a working snippet:
    Qt Code:
    1. void MainWindow::FillData() {
    2. qDebug() << "will try to fill some data now";
    3. stdModel = new QStandardItemModel();
    4. stdModel->setHorizontalHeaderItem(0,new QStandardItem(QString("Col1")));
    5. stdModel->setHorizontalHeaderItem(1,new QStandardItem(QString("Col2")));
    6. stdModel->setHorizontalHeaderItem(2,new QStandardItem(QString("Col3")));
    7.  
    8. for (int i=0;i<5;i++) {
    9.  
    10. QList<QStandardItem*>newRow;
    11.  
    12. QStandardItem *stItem1 = new QStandardItem("A" + QString::number(i));
    13. newRow.append(stItem1);
    14.  
    15. QStandardItem *stItem2 = new QStandardItem("B" + QString::number(i));
    16. newRow.append(stItem2);
    17.  
    18. QStandardItem *stItem3 = new QStandardItem("C" + QString::number(i));
    19. newRow.append(stItem3);
    20.  
    21. stdModel->appendRow(newRow);
    22. }
    23.  
    24. ui->tableView->setModel(stdModel);
    25. }
    To copy to clipboard, switch view to plain text mode 

    and here is a non working one (result is empty QTableView, but it has 5 rows and 3 columns):
    Qt Code:
    1. void MainWindow::FillData() {
    2. qDebug() << "will try to fill some data now";
    3. stdModel = new QStandardItemModel();
    4. stdModel->setHorizontalHeaderItem(0,new QStandardItem(QString("Col1")));
    5. stdModel->setHorizontalHeaderItem(1,new QStandardItem(QString("Col2")));
    6. stdModel->setHorizontalHeaderItem(2,new QStandardItem(QString("Col3")));
    7.  
    8. for (int i=0;i<5;i++) {
    9.  
    10. QList<QStandardItem*>newRow;
    11.  
    12. QStandardItem stItem1("A" + QString::number(i));
    13. newRow.append(&stItem1);
    14.  
    15. QStandardItem stItem2("B" + QString::number(i));
    16. newRow.append(&stItem2);
    17.  
    18. QStandardItem stItem3("C" + QString::number(i));
    19. newRow.append(&stItem3);
    20.  
    21. stdModel->appendRow(newRow);
    22. }
    23.  
    24. ui->tableView->setModel(stdModel);
    25. }
    To copy to clipboard, switch view to plain text mode 
    So as title goes, why do I need to create QStandardItem on the heap. Why do I need this object after it was appended to QStandardModel? I just want to make clear this. Much thanks.

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    As simple as All the stack items will get deleted when you come out of for loop & model is trying ti get data from items but there is no items.
    Thanks :-)

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    Quote Originally Posted by prasad_N View Post
    As simple as All the stack items will get deleted when you come out of for loop & model is trying ti get data from items but there is no items.
    I know, but why do I need them ( I mean QStandardItems), I've added what I want to QStandardItemModel, which is still in scope and on the heap.

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    but model do not hold a data, model will take data (while displaying) from items which are invalid.
    Thanks :-)

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    Quote Originally Posted by prasad_N View Post
    but model do not hold a data, model will take data (while displaying) from items which are invalid.
    Yes you're right. But how do I make sure I don't cause a memory leak in this case. How would you do it, or better what is good practice in general in this case?

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    You no need to take care of items, model will delete all its items when model gets deleted. all we need to do is just delete model.

    something like this: observe destructor will be called for all the heap items

    you can remove sItem class, I just put it to show you there is no memory leak.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QStandardItemModel>
    4. #include <QTableView>
    5.  
    6. #include <QDebug>
    7.  
    8. class sItem : public QStandardItem
    9. {
    10. public:
    11. sItem ( const QString & text = QString() ) : QStandardItem(text)
    12. {
    13. qDebug() << "Const";
    14. }
    15. ~sItem()
    16. {
    17. qDebug() << "Dest";
    18. }
    19. };
    20.  
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QApplication a(argc, argv);
    25. QTableView* view = new QTableView();
    26.  
    27.  
    28. QStandardItemModel* stdModel = new QStandardItemModel(view);
    29. stdModel->setHorizontalHeaderItem(0,new sItem(QString("Col1")));
    30.  
    31. root = stdModel->invisibleRootItem();;
    32.  
    33. for (int i=0;i<5;i++) {
    34. sItem *stItem1 = new sItem("A" + QString::number(i));
    35. root->appendRow(stItem1);
    36. }
    37.  
    38. view->setModel(stdModel);
    39. //view->show();
    40.  
    41. delete view;
    42.  
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

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

    Default Re: Why object on the heap for QStandardItemModel?

    The QStandardItems added to the QStandardItemModel are owned by the model and deleted when it is destroyed. See QStandardItemModel::setItem(). You can reclaim ownership of the items with the QStandardItemModel::take* methods;

  8. #8
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    all we need to do is just delete model
    does deleting QTableView delete the QStandardModel it was assinged?

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    No, because a model can be used by more than one view.

    Cheers,
    _

  10. #10
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    Quote Originally Posted by anda_skoa View Post
    No, because a model can be used by more than one view.

    Cheers,
    _
    Hmmm. I create QStandardItem and append it to QStandardModel. By the same analogy I create QTableView and append QStandardModel to it. But deleting QStandardModel deletes his QStandardItems, while deleting QTableView doesn't delete QStandardModel. Please explaing logic behind this, or how can I know in which cases deleting "parent" object will delete "child" object, much thanks.

  11. #11
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    QTableView* view = new QTableView();
    QStandardItemModel* stdModel = new QStandardItemModel(view);

    I set view as parent to the model that is why model also getting deleted when i delete view.
    (when you delete an object in qt, all it's child also will get deleted automatically, for more info check qt memory management in google).

    if you create model with out parent,(like this: QStandardItemModel* stdModel = new QStandardItemModel(view) you need to delete the model explicitly (delete stdModel) so that it will delete all its items.
    Thanks :-)

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    Quote Originally Posted by arcull View Post
    Hmmm. I create QStandardItem and append it to QStandardModel.
    Yes

    Quote Originally Posted by arcull View Post
    By the same analogy I create QTableView and append QStandardModel to it.
    You tell the view which model to ask for data.

    Quote Originally Posted by arcull View Post
    QStandardModel deletes his QStandardItems
    Yes, it owns the items.

    Quote Originally Posted by arcull View Post
    while deleting QTableView doesn't delete QStandardModel
    Yes, it doesn't own it.

    Quote Originally Posted by arcull View Post
    Please explaing logic behind this, or how can I know in which cases deleting "parent" object will delete "child" object, much thanks.
    In the case of QStandardItemModel you could indeed consider the model to be parent of the items.
    You know that it deletes its items because its documentation says so.

    A view does not own a model, it also does not have any parent/child relationship with the model.
    Since a model can be used by multiple views, none of these views can assume it is the only one and delete the model on its own destruction.
    If you, as the application developer, know that a certain model/view pair is stand-alone, you can just pass the view as the model's parent and let the QObject parent/child handling delete the model upon view deletion.

    Cheers,
    _

  13. #13
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    A view does not own a model, it also does not have any parent/child relationship with the model.
    No but a model can "get" a parent, when creating it, that's why the deltion of table deletes the model as well. I guess prasad_N is right on this one. Thanks both. I think I got the point.

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    Quote Originally Posted by arcull View Post
    No but a model can "get" a parent, when creating it, that's why the deltion of table deletes the model as well.
    QAbstractItemModel is a QObject derived class, so yes, it can have a parent that deletes it as its child.
    That parent can be a view.

    Also wrote that already in my previous comment, last sentence.

    Cheers,
    _

  15. #15
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why object on the heap for QStandardItemModel?

    Also wrote that already in my previous comment, last sentence.
    I've overlooked that, sorry.

Similar Threads

  1. TypeError: Object [object Object] has no method 'sendData'
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 30th November 2013, 05:54
  2. heap corrupted
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2009, 02:54
  3. stack, heap and C#
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th August 2007, 18:40
  4. Heap Error
    By avis_phoenix in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2006, 03:15
  5. QThread and heap
    By paranoid_android in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 10:13

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.