1

I just want to process on database and add the result to a model and send it to another class and view it in GUI. Abstract code is:

I have a public class member:

QSqlQueryModel  *model;

Load data and add it to the model and return model:

QSqlQueryModel* PersistenceAdapter::loadServerList(){
    cout<<"Loading data"<<endl;
    QSqlQuery* qry = new QSqlQuery(db);
    qry->prepare("select * from student1.SERVERLIST");
    model = new QSqlQueryModel();
    model->setQuery(*qry);
    return model;
}

In other class I have a load list function. Error is coming from here:

void MainWindow::setServersList(QSqlQueryModel *myModel) {
    widget.serverListView->setModel(myModel);
}

Then I call it from constructor of same class and here is code:

MainWindow::MainWindow() {
    //Stablish connection to database
    PersistenceAdapter *p = new PersistenceAdapter();
    setServersList(p->loadServerList());
}

And error is:

RUN FINISHED; Segmentation fault; core dumped; real time: 210ms; user: 10ms; system: 40ms

Appreciate if anyone can help.

2
  • What is widget, and are you sure its serverListView has already been initialized? Commented May 5, 2014 at 9:50
  • Bernard, what was the crash reason? It seems that something in the troyane's answer, but I cannot personally understand that. Are you? If yes, could you please elaborate? It seems likely that the reason is more of what we wrote with Mat, so I am surprised. I cannot yet understand how that answer solved the crash. Commented May 6, 2014 at 6:16

2 Answers 2

1

There are several mistakes in your code. Also, here are some tips'n'tricks for your situation:

  1. Are you sure that your model member was initialized/instantiated? To check it just do if model!=NULL check.
  2. Are you sure that your db (in PersistenceAdapter::loadServerList()) was instantiated? _To check it just do if model!=NULL check. Also, check if that db was successfully opened, and check if it isOpen at moment of use. Keep in mind that you can got some errors while doing multithreaded database access.
  3. I do not recommend create QSqlQuery, instead you can use another setQuery method.
  4. After you apply your query, do check: if (model.lastError().isValid()) qDebug() << model.lastError();
  5. Check order of calling your functions -- if instantiating db goes earlier than accessing db and so on.

Hope this tips will throw light upon. Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

1) if you check the code carefully, you can see that it is. 2) If it was not instantiated, you would get a compiler error, so it seems to be instantiated. The rest could not cause crash. 3) it seems to be called only from the constructor, so it is only executed once. What is the problem there? 4) True, but not crash reason. :) 5) seem to be repeating 2). -> Btw, IMHO, you have not pointed the actual crash out, or I might be wrong. Can you point out which recommendation would solve the crash?
0

widget.serverListView->setModel(myModel);

So, the crash can only be there. Since the widget seems to be on the heap (which is a bad idea), the only reason for the crash is that your server list view is not properly initialized at that stage, so it is either a null pointer or dangling.

This also looks logical since you do not seem to initialize the view of the widget in your main window constructor. If you do it so, the crash will go away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.