Results 1 to 10 of 10

Thread: App crashing on addRow to QStandardItemModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    97
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 11 Times in 11 Posts

    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.

  2. #2
    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 

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

    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 19:31.

  4. #4
    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 

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

    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 

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

    homerun4711 (28th December 2010)

  7. #6
    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, 18:01
  2. QStandardItemModel insertRow crashing
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 12:55
  3. Qt 4.4 Linguish Crashing on XP?
    By DerSchoeneBahnhof in forum Qt Tools
    Replies: 0
    Last Post: 19th June 2008, 23:42
  4. QPageSetupDialog is crashing always
    By senthilsp in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 11:21
  5. QMultiHash - crashing
    By steg90 in forum Qt Programming
    Replies: 16
    Last Post: 23rd May 2007, 14: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
  •  
Qt is a trademark of The Qt Company.