我對Qt相當新,也許這就是爲什麼我不能完全理解孩子父母的概念。我需要執行一些SQL查詢。我設置了QSqlQuery,執行「準備和綁定」操作並執行它。接下來,我將它傳遞給模型並顯示數據。關閉窗口時發生問題 - 我收到內存違例錯誤。僅當我使用父級創建模型時,纔會出現錯誤。下面的代碼:QSqlQueryModel與父 - 應用程序崩潰
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();
這裏的,我發現了錯誤:
Unhandled exception at 0x0fe29f9a (qsqlpsqld.dll) in HurBudClientGUI.exe: 0xC0000005: Access violation reading location 0x00000004.
和調用堆棧:
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++
正如我上面提到:錯誤不會發生時, (以下之一):
- 我創建QSqlQueryModel沒有parent(model = new QSqlQueryModel;)
- 我將「靜態」查詢傳遞給QSqlQueryModel(不管是否有父級)。
如:
model->setQuery(
QSqlQuery(
QString("SELECT \
%1 as nazwa \
, kontrahentid \
FROM kontrahent"
).arg(showWhat)
)
);
我在做什麼錯? 真正的問題是:QSqlQueryModel有父級的目的是什麼?如果我在窗口的析構函數中手動刪除它 - 是否有任何差異?
我想這是一個錯誤 - 我報告說,它在QT錯誤追蹤: https://bugreports.qt.io/browse/QTBUG-43889
閱讀關於'Qt's父/子的概念:http://qt-project.org/doc/qt-4.8/objecttrees.html ...你的代碼,你是否刪除了析構函數中的模型? – Zaiborg 2014-09-04 09:09:25
我讀過它......沒有找到對我的情況有用的東西。是的,我在析構函數中手動調用delete(如上一句中提到的) – murison 2014-09-04 09:53:05
這是你的問題。一旦你讓它成爲一個孩子,你無法在任何地方刪除模型。當父母被釋放時,父母會爲你刪除它。 – drescherjm 2014-09-04 12:34:59