[英]Program Crashes with Qt QAbstractTableModel::index
我正在嘗試在qt中實現自定義模型。
我已經將QAbstractTableModel子類化為自己的類。 我已經重新實現了所需的方法,但沒有實現索引方法(如文檔所說)。 columnCount()始終返回4(列數固定),而rowCount返回插入的行數。
我的程序如下所示:
應用程序
#include mymodel.h
class ContilasSimulator : public QApplication
{
public:
Aplication(int argc, char *argv[]);
void someFunction();
private:
MyModel m_model;
MainWindow m_window;
};
application.cpp
#include application.h
Application::Application(int argc, char *argv[]) : QApplication(argc,argv),
{
m_window.setModel(&m_model);
}
void someFunction()
{
//...
if (m_model.insertRows(0,2))
{
QModelIndex index = m_model.index(0,0); //this statement works fine
index = m_model.index(1,0); // this statement also works fine
index = m_model.index(0,5); //this statements return an invalid index (as expected)
index = m_model.index(0,1); //the program crashes a few seconds after executing this line
//other code...
}
僅在嘗試獲取m_model.index(0,1)行之后,程序才會崩潰,但不會立即崩潰(即,接下來的幾行將執行,但幾秒鍾后,程序崩潰)。 當與調試器逐行進行時,程序將在探針行之后的行中崩潰,或者在以后的幾行中崩潰,這取決於我執行的速度。 我收到此錯誤消息:
ASSERT failure in QList<T>::at: "index out of range", file C:\Qt\Qt5.5.1\5.5\mingw492_32\include/QtCore/qlist.h, line 510
我無法弄清楚為什么我請求索引(0,0)和(1,0)而不是(0,1)時程序能正常工作。 我也無法弄清楚為什么它不會立即失敗,而是需要幾秒鍾的時間才能失敗。 我沒有對線程做任何事情。
對於為什么會出現此問題或我可以采取的進一步調試步驟的任何幫助,將不勝感激。
我正在將Qt 5.5與Qt Creator 3.4.2和mingw一起使用
您正在嘗試訪問無效索引...這是未指定的行為...如果參數瘋狂,則qabstractitemmodel
某些實現將返回無效索引。 但這不能保證。
在這種情況下,發生的情況是表模型在后台使用了QList,並且不必費心進行范圍檢查...這會導致訪問范圍外。 我懷疑,即使Qt沒有說出它是什么,超出范圍的訪問也是未定義的行為 。
無論如何,這是錯誤的。 僅訪問有效索引 。 故事的結局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.