PDA

View Full Version : QGraphicsItem move influences other items in scene



esotery
26th November 2012, 23:32
Hello,

I have come across some behaviour of Scene (or maybe Item) that I don't like and I don't know how to get rid of it.

I have Scene with two items. They are both Movable and Selectable. They are stacked on each other and if I move the top Item to whatever direction, it will also move the other Item that is beneath it to opposite direction.

The code is really simple:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QGraphicsScene* scene = new QGraphicsScene(this);
QGraphicsView* view = new QGraphicsView(this);
view->setScene(scene);
view->setMinimumSize(400, 200);

QGraphicsRectItem* item1 = new QGraphicsRectItem(0, 0, 50, 50);
item1->setFlag(QGraphicsItem::ItemIsMovable, true);
item1->setFlag(QGraphicsItem::ItemIsSelectable, true);
QGraphicsRectItem* item2 = new QGraphicsRectItem(0, 0, 50, 50);
item2->setFlags(item1->flags());

scene->addItem(item1);
scene->addItem(item2);

setCentralWidget(view);
}


I know it must be something stupid that I've overlooked.

Thank you for your help.

Small compilable example attached.

amleto
27th November 2012, 01:50
the only 'automatic' way that one item can move another is if they are in child/parent relationship. Since this isn't the case here, I don't know why moving one would move the other.

can you show some debug logs of position that shows both items moving after manually moving only one?

wysota
27th November 2012, 10:23
My wild guess is that you are moving the item outside scene boundaries and since you didn't set the scene dimensions, the scene size is dynamically modified by the framework to encompass all items in the scene. Thus if the scene expands to the left when you move one item to the left, the other item is virtually moved to the right because the scene is to remain centered in the view. In reality the item is not moved to the right, the whole scene is moved in the view.