Update scene
-
@mandruk1331
But what u want to swap them?
Is it not enough to just swap the colors?QPointF Pos1;
Pos1=rectan[0]->pos();
QPointF Pos2;
Pos2=rectan[1]->pos();
rectan[0]->setPos(pos2);
rectan[1]->setPos(pos1); -
-
@mrjj No, at the moment I'm trying to make smth like an Update func
void MainWindow::Shot(){
QGraphicsRectItem *Pos1;
Pos1=rectan[0];
QGraphicsRectItem *Pos2;
Pos2=rectan[1];
scene->removeItem(rectan.at(0));
scene->removeItem(rectan.at(1));rectan.at(0) = Pos2;
rectan.at(1) = Pos1;
scene->addItem(rectan.at(0));
scene->addItem(rectan.at(1));
} -
Hi
at returns const.
This looks strange to me
rectan.at(0) = Pos2;
maybe
rectan[0] = Pos2Also this will just swap the position in your list.
It will NOT change on screen.You need to use pos() and SetPos
Not just swap in you array. You swap pointers.
You should swap QPointF using pos and setPos; -
Yes I guess it will repaint them on scene.
But it matters not for scene if you swap your own array.
The items still have same location as before as you do not setPos
(from code shown) -
@mrjj and another one, I have a loop inside of a loop and when the second one finishes its iteration it won't singleshot on another iteration of the first loop
for (int =0;i<10;i++){
//and when i is 1 the second loop won't singleshot again
for(int j=0;j<9;j++){
SingleShot
}
} -
well
Normally it works as many times as u set it up but hard to guess at with
only "SingleShot". -
-
@mandruk1331 said:
Why do yo u give rectan.at(0) to constructor ?
new QGraphicsRectItem(rectan.at(0));Try
scene->addItem( new QGraphicsRectItem );
and tell if that not add a new item? -
@mrjj Ok, I managed to swap positions of the rectangles, but they swap not always, at first execution they swap on the other they don't, what could be the problem??
void MainWindow::Shot(){
QPointF Pos1;
QPointF Pos2;
Pos1 = rectan.at(5)->pos();
Pos2 = rectan.at(6)->pos();rectan.at(6)->setBrush(Qt::blue);
rectan.at(5)->setBrush(Qt::green);
// rectan.at(5)->setBrush(Qt::blue);
rectan.at(6)->setPos(Pos1.rx()-30,Pos1.ry());
rectan.at(5)->setPos(Pos2.rx()+30,Pos2.ry());}
Solved it) -
hi
put qDebug() << "in shot:"
in the
MainWindow::Shot()
to make sure its actually called mutiple times. -
if pos() returns 0,0 , it must mean the items is actually at 0,0.
-
@mandruk1331
Something is not right.
Unless they are all really drawn at 0,0 then pos() should return the
actual position.
Did you new a QGraphicsRectItem and inserted directly in the list? -
Ok, then they should still have the pos, unless you swapped with a
item having 0,0.