I am a bit frustrated with seg faults.
I have the following main
int main(int argc, char *argv[])
{
bool connection;
if (!createConnection())
connection = false;
else
connection = true;
qDebug()<<"connection: "<<connection;
mainWindow start(connection,0);
start.show();
return app.exec();
}
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();
}
To copy to clipboard, switch view to plain text mode
and the connection.h is
inline bool createConnection()
{
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
Now, in mainWindow.cpp I have a QTableView tied with a database:
model->setQuery("SELECT * FROM persone");
...
listTable->setShowGrid(false);
listTable->setModel(model);
...
}
void mainWindow
::updateDetails(const QModelIndex &selection
) {
/* Get the selected indexes in the TableView*/
if (selection.isValid()) {
qDebug() << selection.row();
// QString temp = record.value("nome").toString();
}
}
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();
}
}
To copy to clipboard, switch view to plain text mode
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 ()
\#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 ()
To copy to clipboard, switch view to plain text mode
Bookmarks