PDA

View Full Version : Seg fault QSqlRecord



giusepped
24th October 2008, 08:29
I am a bit frustrated with seg faults.

I have the following main


int main(int argc, char *argv[])
{
bool connection;


QApplication app(argc, argv);

if (!createConnection())
connection = false;

else
connection = true;
qDebug()<<"connection: "<<connection;
mainWindow start(connection,0);
start.show();
return app.exec();
}


and the connection.h is



inline bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("ppl.db");
if (!db.open()) {
QMessageBox::critical(0,QObject::tr("Non riesco a connettermi al database. Controlla il nome o verifica che il file sia presente"),db.lastError().text());
return false;
}
return true;
}


Now, in mainWindow.cpp I have a QTableView tied with a database:



QSqlQueryModel *model = new QSqlQueryModel(this);
model->setQuery("SELECT * FROM persone");
...
QTableView *listTable = new QTableView(this);
listTable->setShowGrid(false);
listTable->setModel(model);
...
connect(listTable,SIGNAL(clicked(const QModelIndex &)),this,SLOT(updateDetails(const QModelIndex &)));
}

void mainWindow::updateDetails(const QModelIndex &selection)
{
/* Get the selected indexes in the TableView*/
if (selection.isValid()) {
qDebug() << selection.row();
QSqlRecord rec = model->record(0);

// QString temp = record.value("nome").toString();
}

}

The compilation is good, but when i click on the first row of the Table a seg fault is fired.
Any help?
Attached is an excerpt of the gdb output:



\#0 QSqlRecord (this=0xbf8a4388, other=@0x16d45a8) at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:122
\#1 0x0082d40c in QSqlQueryModel::record (this=0xbf8a54d8, row=0) at models/qsqlquerymodel.cpp:481
\#2 0x0804b726 in mainWindow::updateDetails ()

giusepped
24th October 2008, 09:33
It seems that if I declare



model = new QSqlQueryModel(this);


all works.
Which is the difference?

Kumosan
24th October 2008, 09:53
It seems that if I declare



model = new QSqlQueryModel(this);


all works.
Which is the difference?

Outch, this happens when only a part of the code is posted. But what I do not understand is why your code compiles at all.

You do a


QSqlQueryModel *model = new QSqlQueryModel(this);

in your constuctor? Don't know, this part is missing. However, your *model is a local variable, which has absolutely nothing to do with the model in updateDetails(...). Wonder what 'model' the compiler finds in updateDetails(...) at all. Or you have a model pointer as member variable. However, this is not the one you initialize in your constructor.

spirit
24th October 2008, 09:53
because you have a class member variable model and it was not initilazed.
in constructor you create a variable with the same name


...
QSqlQueryModel *model = new QSqlQueryModel(this);
...


and when you try to access to methods in a model (it is still isn't initilazed) you get seg fault, because model variable has wrong pointer.

giusepped
25th October 2008, 06:03
Solved.Thank you.
In fact, in the header I defined


QSqlRecord *model;

That was the error.
step by step I am going to get accustomed to C++.
Bye