PDA

View Full Version : Error:Assert Failure QLIst<t> index out of range



hema
27th July 2011, 06:21
hi,

my app has some balloons with alphabets A-Z as text. I dont want to repeat the balloon with same character,I retrieved items from scene to QList.after completion of all letters,my app has to halt.but in middle itself it is giving error of Assert failure index out of range.can anyone help in correction of error

my code:
void GraphicsScene::createBallon(void)
{
QList<QGraphicsItem *>itemno=items();
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
int random = qrand() % 26;
for(int i=1;i<itemno.size();i++)
{
Ballon *temp=qgraphicsitem_cast<Ballon*>(itemno.at(i));
if(temp->getText()==str[random])
return;
}
Ballon *ball = new Ballon(str[random]);
ball->setPos(500 , 550);
addItem(ball);
if(itemno.size()>26)
interval->stop();
}

stampede
27th July 2011, 07:37
What is str ? My first guess after quick glimpse at this code is that str has less elements than random value, so you are trying to access element out of range.
Btw. this code is not safe:


Ballon *temp=qgraphicsitem_cast<Ballon*>(itemno.at(i));
if(temp->getText()==str[random])
return;

What if at some point you will insert items that are not "ballons" ? temp will be NULL and calling temp->getText() will cause crash. Check it like this:


Ballon *temp=qgraphicsitem_cast<Ballon*>(itemno.at(i));
if(temp and temp->getText()==str[random])
return;

ChrisW67
27th July 2011, 08:33
At line 6 you start your indexes at 1 rather than the more conventional 0. Is that deliberate?

hema
27th July 2011, 09:30
hi,

thanks to all.my problem is solved.i gave length of str is lesser than the random value.
i want to display tis graphical ballons in circular way. how can i try this?
i used setPos code its displaying at top of view that too half cut.how can i get it in middle of the scene?
setPos(::sin((i * 6.28) /26) * 200,::cos((i * 6.28) /26) * 200);