PDA

View Full Version : GUI parametres update in advance slot



galvanize
9th May 2013, 07:16
Hi,

I try to update QLCDNumber value( which is placed, of course, out of QGraphicScene) in advance function, which is part of one of QGraphicsItem. Program breaks. Whereas if I place it for example in constructor everything is fine, but of course it works only once.

Definition

QLCDNumber *elevatorsCurrentLevelDisplayArray[5];
{
elevatorsCurrentLevelDisplayArray[0]=ui->elevator1;
elevatorsCurrentLevelDisplayArray[1]=ui->elevator2;
elevatorsCurrentLevelDisplayArray[2]=ui->elevator3;
elevatorsCurrentLevelDisplayArray[3]=ui->elevator4;
elevatorsCurrentLevelDisplayArray[4]=ui->elevator5;
}
QLCDNumber **elevatorsCurrentLevelDisplay = elevatorsCurrentLevelDisplayArray;

Here it works

QGraphicsBuilding::QGraphicsBuilding(QLCDNumber **_elevatorsCurrentLevelDisplay)
{
(*mv_elevatorsCurrentLevelDisplay)[0].display(3);
}


Here it breaks.

void QGraphicsBuilding::advance(int step)
{
if (!step)
return;
//for(int i=0; i<m_building->m_numberElevators; i++)
(*mv_elevatorsCurrentLevelDisplay)[0].display(3);
addQGraphicsMen();
m_building->simulation();
updateCoordinates();
updateParents();
}

wysota
9th May 2013, 07:44
Get rid of all the pointers and the error will probably either go away by itself or will give you a clean reason of why it is not working (e.g. trying to access the pointer array before it is assigned proper data). If the first snippet of your code is your actual code then what you do is that you create a local array of pointers to QLCDNumber then you assign this array to another pointer and then you leave the scope, causing the first array to be deleted by the compiler and you are left with dangling pointer. As long as something overwrites the part of the stack where the array was declared (which most probably happens after you call a method or two causing the top of the stack to move), when you try to access data in the array that was deleted, your application crashes. Instead of this incorrect construction, declare a list of pointers to QLCDNumber as a member of your class and access it directly whenever you need it.

galvanize
9th May 2013, 08:40
Thank You very much. Now everything is fine.