2012-11-23 321 views
0

使用定時器我重複調用插槽checkBookings()。 我能夠編譯和運行該程序,但在執行上述FOR循環時會崩潰。QList索引超出範圍

Error:"ASSERT failure in QList::at: "index out of range", file ../../../../Qt/2010.05/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 455 Invalid parameter passed to C runtime function. Invalid parameter passed to C runtime function."

我的代碼是:

timer = new QTimer(); 
connect(timer,SIGNAL(timeout()),this,SLOT(checkBookings())); 
timer->start(500000); 

void Canvas::checkBookings() 
{ 

QString dateStr; 

for(int i= 0;i<=qlist.count();i++) 
    { 
     dateStr = qList.at(i).at(6); 
    } 

} 
+0

除非您顯示變量qlist是什麼以及如何添加或刪除元素,否則這不會被回答。 – cgmb

回答

5

在您更換< =與< for循環。就像這樣:

for(int i= 0;i<qlist.count();i++) 
{ 
    dateStr = qList.at(i).at(6); 
} 

原因是qList.count()是項目的列表,所以你再也不想嘗試使用qList.at(qlist.count())

數編輯: 通過在for循環中有< =記住for循環的最後一次迭代i = qlist.count()。那麼當代碼在循環中執行語句時,它基本上是這樣做的:

dateStr = qList.at(qList.count()).at(6); 
+0

然後,也許它是可以回答的。接得好。 – cgmb