سؤال

Using a timer I call the slot checkBookings() repeatedly. I am able to compile and run the program, but it crashes when executing the above FOR loop.

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."

My code is:

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);
    }

}
هل كانت مفيدة؟

المحلول

Replace <= with < in your for loop. Like this:

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

The reason is qList.count() is the number of items in the list so you do not ever want to try to use qList.at(qlist.count())

Edit: By having <= in the for loop remember that the at last iteration of the for loop i=qlist.count(). So then when the code executes the statement in the loop it essentially does this:

dateStr = qList.at(qList.count()).at(6);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top