Results 1 to 10 of 10

Thread: App crashing on addRow to QStandardItemModel

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Question App crashing on addRow to QStandardItemModel

    Hi!

    I try to add values from a map to a QStandardItemModel, but my application is crashing. I have no idea why..

    The last debug output is

    QStandardItemModel::invisibleRootItem() const
    The code is

    Qt Code:
    1. QMap<QString, QString> map;
    2. QMapIterator<QString, QString> i(map);
    3. QList<QStandardItem*> rowList;
    4. QStandardItem *item = test->invisibleRootItem();
    5.  
    6.  
    7. while (i.hasNext()) {
    8. i.next();
    9. rowList << new QStandardItem(i.value());
    10. }
    11.  
    12. item->appendRow(rowList);
    To copy to clipboard, switch view to plain text mode 

    Can you tell me why this happens?

    Kind regards,
    HomeR

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: App crashing on addRow to QStandardItemModel

    You are defining 'test' pointer, but not initializing it, but you call invisibleRootItem() on it - no wonder it crashed!
    Qt Code:
    1. QStandardItemModel* test; //<<-- not initialized
    2. QStandardItem *item = test->invisibleRootItem(); //crash!
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    homerun4711 (28th December 2010)

  4. #3
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: App crashing on addRow to QStandardItemModel

    You are right.
    Sometimes I am dumb and blind

  5. #4
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: App crashing on addRow to QStandardItemModel

    Hm, there is something else going wrong...could you please have a look into the code? I initialized the pointer but the application is still crashing at the same spot.

    mainwindow.h

    Qt Code:
    1. QStandardItemModel *model_customer;
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ...
    5. setupModels();
    6. ...
    7. }
    8.  
    9. void MainWindow::setupModels()
    10. {
    11.  
    12.  
    13. QStandardItemModel *model_customer = new QStandardItemModel;
    14.  
    15. }
    16.  
    17. void MainWindow::newcustomer() //called with a QPushButton
    18. {
    19.  
    20. AddressNew *newcustomer = new AddressNew(this);
    21. if (newcustomer->exec())
    22. {
    23. newcustomer->addToModel(model_customer);
    24. }
    To copy to clipboard, switch view to plain text mode 

    AddressNew.h

    Qt Code:
    1. void setMap(QStandardItemModel* model);
    To copy to clipboard, switch view to plain text mode 

    AddressNew.cpp

    Qt Code:
    1. void AddressNew::setMap(QStandardItemModel* model)
    2.  
    3. QMap<QString, QString> map; //contains QStrings from QLineEdits
    4. QMapIterator<QString, QString> i(map);
    5. QList<QStandardItem*> rowList;
    6. QStandardItem *item = model->invisibleRootItem();
    7.  
    8. while (i.hasNext()) {
    9. i.next();
    10. rowList << new QStandardItem(i.value());
    11. }
    12.  
    13. item->appendRow(rowList);
    To copy to clipboard, switch view to plain text mode 

    Any ideas?

  6. #5
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: App crashing on addRow to QStandardItemModel

    Quote Originally Posted by homerun4711 View Post
    Qt Code:
    1. void AddressNew::setMap(QStandardItemModel* model)
    2.  
    3. QMap<QString, QString> map; //contains QStrings from QLineEdits
    4. QMapIterator<QString, QString> i(map);
    5. QList<QStandardItem*> rowList;
    6. QStandardItem *item = model->invisibleRootItem();
    To copy to clipboard, switch view to plain text mode 
    Well, now you've changed from getting the root item from 'test' to getting it from 'model', which is a parameter; 'test' now not being used. The problem is probably in calling code, which you haven't provided. May very well be exactly the same problem as before: invalid pointer.

  7. #6
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: App crashing on addRow to QStandardItemModel

    Well, I tried to add the row to the model using

    Qt Code:
    1. model->appendRow(rowList);
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. item->appendRow(rowList);
    To copy to clipboard, switch view to plain text mode 

    but the application is crashing anyway...

    I tried to stick to code I found here (see 3.1 TreeView)

    http://doc.troll.no/master-snapshot/modelview.html

    Qt Code:
    1. standardModel = new QStandardItemModel ;
    2. QList<QStandardItem *> preparedRow =prepareRow("first", "second", "third");
    3. QStandardItem *item = standardModel->invisibleRootItem();
    4. // adding a row to the invisible root item produces a root element
    5. item->appendRow(preparedRow);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: App crashing on addRow to QStandardItemModel

    Quote Originally Posted by homerun4711 View Post
    Well, I tried to add the row to the model using

    Qt Code:
    1. model->appendRow(rowList);
    To copy to clipboard, switch view to plain text mode 

    instead of

    Qt Code:
    1. item->appendRow(rowList);
    To copy to clipboard, switch view to plain text mode 

    but the application is crashing anyway...
    Which leads me to believe I'm probably right and the problem is that 'model' is not initialized to mean anything. We can't tell for sure at this point though because the code that would be responsible for creating it, whatever is calling your AddressNew::setMap function, is not part of the code you provided in your question.

    Edit:

    Nevermind.... I'm wrong. You did provide that code; I just didn't see it. Your symptoms would lead me to believe I'm right but unless something is coming along and hosing your model_customer or something I'm not. You might just break in that function and make sure *model is something valid just to be sure though.
    Last edited by nroberts; 28th December 2010 at 18:31.

  9. #8
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: App crashing on addRow to QStandardItemModel

    Ok, we are getting closer You are right, maybe the pointer is not
    initialized when it is needed.
    AddressNew::setMap is called (AddressNew is a QDialog) when the Dialog is
    accepted.

    Qt Code:
    1. AddressNew *newcustomer = new AddressNew(this);
    2.  
    3. if (newcustomer->exec())
    4. {
    5. newcustomer->setMap(model_customer);
    6. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: App crashing on addRow to QStandardItemModel

    Quote Originally Posted by homerun4711 View Post
    Ok, we are getting closer You are right, maybe the pointer is not
    initialized when it is needed.
    AddressNew::setMap is called (AddressNew is a QDialog) when the Dialog is
    accepted.

    Qt Code:
    1. AddressNew *newcustomer = new AddressNew(this);
    2.  
    3. if (newcustomer->exec())
    4. {
    5. newcustomer->setMap(model_customer);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Oh shit, I know where your problem is. Should have seen it before.

    See your second cry for help post: http://www.qtcentre.org/threads/3736...700#post171700

    In the first code segment you create an apparently global model_customer variable. Even if it's not global but is part of the class definition, the problem is still the same: name resolution applied to scoping rules.

    Later, in setupModels() you do what kind of looks like an initialization of that variable, but it isn't. Because you provide the type before the variable name (QStandardItemModel*) you are actually declaring a new variable within a smaller scope, overriding the former. Thus the former, global version is never initialized and the pointer you DO initialize silently goes away, creating a memory leak BTW. Then when you call setMap you pass the former, global, uninitialized variable.

    To fix your code, change this:

    Qt Code:
    1. void MainWindow::setupModels()
    2. {
    3.  
    4.  
    5. QStandardItemModel *model_customer = new QStandardItemModel;
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    To this:

    Qt Code:
    1. void MainWindow::setupModels()
    2. {
    3.  
    4.  
    5. model_customer = new QStandardItemModel;
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to nroberts for this useful post:

    homerun4711 (28th December 2010)

  12. #10
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: App crashing on addRow to QStandardItemModel

    Oh my god, you are SO right!!!
    Thank you very much! Exactly this was the problem.
    So much to learn...

Similar Threads

  1. Crashing without qDebug() ?
    By xtreme in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 17:01
  2. QStandardItemModel insertRow crashing
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 11:55
  3. Qt 4.4 Linguish Crashing on XP?
    By DerSchoeneBahnhof in forum Qt Tools
    Replies: 0
    Last Post: 19th June 2008, 22:42
  4. QPageSetupDialog is crashing always
    By senthilsp in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 10:21
  5. QMultiHash - crashing
    By steg90 in forum Qt Programming
    Replies: 16
    Last Post: 23rd May 2007, 13:18

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.