我试图在qt中实现一个自定义模型。
我已经将QAbstractTableModel子类划分到我自己的类中。我重新实现了所需的方法,但没有实现索引方法(正如文档所说)。columnCount()总是返回4(列数固定),rowCount返回安装行数。
我的程序看起来是这样的:
application.h
#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)行之后,程序才会崩溃,但不是一成不变的(即,接下来的几行将执行,但几秒钟后程序崩溃)。当使用我的调试器逐行运行时,程序将崩溃,或者是在probelm行之后崩溃,或者是在几行之后崩溃,这取决于我通过的速度。我收到以下错误消息:
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)时,我不知道为什么程序工作得很好。我也不知道为什么它不会马上失败,而是需要几秒钟才能失败。我不会用线做任何事。
任何帮助,我可能会有这个问题或进一步的调试步骤,我可以采取将非常感谢。
我使用QT5.5与Qt 3.4.2一起编译
发布于 2016-05-04 16:29:10
您正在尝试访问无效索引..。这是未知的行为..。如果参数是疯狂的,qabstractitemmodel的某些实现将返回无效的索引。但这不能保证..。
在这种情况下发生的情况是,表模型使用了一个QList在引擎盖下,而不用费心做范围检查.这会导致无法绑定的访问。我怀疑超出绑定的访问是一个未定义行为,即使Qt没有说出它是什么。
不管怎样,这是错误的。只访问有效索引。故事的结尾。
https://stackoverflow.com/questions/37032992
复制相似问题