Code for each object.
-
Good day, I'm trying to apply code to each individual object that I create with an onclick listener. At the moment the logic applies to all objects at once regardless of the objects individually. Does anyone know how this can be done?
This is the code I use to create the progressbar objects:
void Bounce::mousePressEvent(QMouseEvent *event) { QProgressBar* stress = new QProgressBar(this); stress->move(LetItGo->x(), LetItGo->y()-stress->height()); stress->show(); stress->setValue(100); worry.push_back(stress); }
This si the code I wanna apply to each individual object:
for(auto stress : worry) { if((stress)->x() >= width()) { flag=1; (stress)->setValue((stress)->value()-10); NrOfBounces++; if(NrOfBounces==10) { } } if((stress)->x() <= 0) { flag=0; (stress)->setValue((stress)->value()-10); NrOfBounces++; if(NrOfBounces==10) { } } if(flag==1) { horizontalspeed = -20; } else if(flag==0) { horizontalspeed = 20; } (stress)->move((stress)->x()+horizontalspeed, (stress)->y()); /*if((stress)->x()+(stress)->height() > height()) { verticalspeed = -3; (stress)->setValue((stress)->value()-10); } else if((stress)->x() < 0) { verticalspeed = 3; (stress)->setValue((stress)->value()-10); }*/ count++; } }
It's basically a function to bounce left when it hits the right edge of the form and bounce right when it hits the left. The problem is if one of the objects hit the left edge of the form, all the other existing objects bounce right along with the one that hit the wall. So I wanna apply this logic individually. Any help would be much appreciated!
-
Hi and welcome to devnet,
When is that code called ?