I am rather new to Qt, maybe that's why I cant fully understand the child-parent concept. I need to perform some sql query. I set the QSqlQuery, perform the "prepare and bind" operation and exec it. Next I pass it to the model and display the data. The problem occurs when closing the window - I get a memory violation error. The error occurs only, when I create the model with a parent. Here's the code:
QSqlQuery query;
query.prepare(QString("SELECT \
%1 as nazwa \
, kontrahentid \
FROM kontrahent WHERE %2 ilike ?"
).arg(showWhat, searchBy) //handled above, no need to escape
);
query.addBindValue(searchString); //user input data - so bind it
if (!query.exec()) {
qDebug() << query.lastError();
QApplication::restoreOverrideCursor();
return;
}
if (model == NULL)
// model = new QSqlQueryModel; // app closes the window correctly
model = new QSqlQueryModel(this); // app crashes when closing the window
model->setQuery(query);
if (model->lastError().isValid()) {
qDebug() << model->lastError();
QApplication::restoreOverrideCursor();
return;
}
model->setHeaderData(0, Qt::Horizontal, "ID");
ui.kontrahenciList->setModel(model);
//ui.kontrahenciList->setModelColumn(1);
ui.kontrahenciList->show();
Here's the error I'm getting:
Unhandled exception at 0x0fe29f9a (qsqlpsqld.dll) in HurBudClientGUI.exe: 0xC0000005: Access violation reading location 0x00000004.
and the call stack:
qsqlpsqld.dll!QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> >::data() Line 143 + 0x3 bytes C++
qsqlpsqld.dll!qGetPtrHelper<QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> > >(const QScopedPointer<QObjectData,QScopedPointerDeleter<QObjectData> > & p) Line 919 + 0xb bytes C++
qsqlpsqld.dll!QPSQLDriver::d_func() Line 106 + 0x13 bytes C++
qsqlpsqld.dll!QPSQLResultPrivate::privDriver() Line 212 C++
qsqlpsqld.dll!QPSQLResultPrivate::deallocatePreparedStmt() Line 306 + 0xc bytes C++
qsqlpsqld.dll!QPSQLResult::~QPSQLResult() Line 328 C++
qsqlpsqld.dll!QPSQLResult::`scalar deleting destructor'() + 0xf bytes C++
Qt5Sqld.dll!QSqlQueryPrivate::~QSqlQueryPrivate() Line 94 + 0x23 bytes C++
Qt5Sqld.dll!QSqlQueryPrivate::`scalar deleting destructor'() + 0xf bytes C++
Qt5Sqld.dll!QSqlQuery::~QSqlQuery() Line 245 + 0x1e bytes C++
Qt5Sqld.dll!QSqlQueryModelPrivate::~QSqlQueryModelPrivate() Line 90 + 0x3d bytes C++
Qt5Sqld.dll!QSqlQueryModelPrivate::`scalar deleting destructor'() + 0xf bytes C++
Qt5Cored.dll!672cbf06()
[Frames below may be incorrect and/or missing, no symbols loaded for Qt5Cored.dll]
Qt5Cored.dll!672cb92a()
Qt5Cored.dll!672c03f4()
Qt5Cored.dll!67200dc4()
Qt5Cored.dll!67203608()
Qt5Sqld.dll!QSqlQueryModel::~QSqlQueryModel() Line 175 + 0x9 bytes C++
As I mentioned above: the error doesn't happen when (one of below):
- I create QSqlQueryModel without the parent (model = new QSqlQueryModel;)
- I pass "static" query to QSqlQueryModel (regardless of having the parent).
eg:
model->setQuery(
QSqlQuery(
QString("SELECT \
%1 as nazwa \
, kontrahentid \
FROM kontrahent"
).arg(showWhat)
)
);
What am I doing wrong? And the real question is: what is the purpose for QSqlQueryModel having a parent? If I delete it manually in the window's destructor - is there any diffrence?
I guess this is a bug - I reported it on qt bugtracker: https://bugreports.qt.io/browse/QTBUG-43889
Qt
s parent/child concept here: qt-project.org/doc/qt-4.8/objecttrees.html ... to your code, did you delete the model in the destructor? – Zaiborg