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
Quote:
QStandardItemModel::invisibleRootItem() const
The code is
Code:
QMap<QString, QString> map;
QMapIterator<QString, QString> i(map);
QList<QStandardItem*> rowList;
while (i.hasNext()) {
i.next();
}
item->appendRow(rowList);
Can you tell me why this happens?
Kind regards,
HomeR
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!
Re: App crashing on addRow to QStandardItemModel
You are right.
Sometimes I am dumb and blind :)
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
mainwindow.cpp
Code:
MainWindow
::MainWindow(QWidget *parent
){
...
setupModels();
...
}
void MainWindow::setupModels()
{
}
void MainWindow::newcustomer() //called with a QPushButton
{
AddressNew *newcustomer = new AddressNew(this);
if (newcustomer->exec())
{
newcustomer->addToModel(model_customer);
}
AddressNew.h
AddressNew.cpp
Code:
QMap<QString, QString> map; //contains QStrings from QLineEdits
QMapIterator<QString, QString> i(map);
QList<QStandardItem*> rowList;
while (i.hasNext()) {
i.next();
}
item->appendRow(rowList);
Any ideas?
Re: App crashing on addRow to QStandardItemModel
Quote:
Originally Posted by
homerun4711
Code:
QMap<QString, QString> map; //contains QStrings from QLineEdits
QMapIterator<QString, QString> i(map);
QList<QStandardItem*> rowList;
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.
Re: App crashing on addRow to QStandardItemModel
Well, I tried to add the row to the model using
Code:
model->appendRow(rowList);
instead of
Code:
item->appendRow(rowList);
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
Code:
QList<QStandardItem *> preparedRow =prepareRow("first", "second", "third");
// adding a row to the invisible root item produces a root element
item->appendRow(preparedRow);
Re: App crashing on addRow to QStandardItemModel
Quote:
Originally Posted by
homerun4711
Well, I tried to add the row to the model using
Code:
model->appendRow(rowList);
instead of
Code:
item->appendRow(rowList);
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.
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.
Code:
AddressNew *newcustomer = new AddressNew(this);
if (newcustomer->exec())
{
newcustomer->setMap(model_customer);
}
Re: App crashing on addRow to QStandardItemModel
Quote:
Originally Posted by
homerun4711
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.
Code:
AddressNew *newcustomer = new AddressNew(this);
if (newcustomer->exec())
{
newcustomer->setMap(model_customer);
}
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:
Code:
void MainWindow::setupModels()
{
}
To this:
Code:
void MainWindow::setupModels()
{
}
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... :)