Results 1 to 3 of 3

Thread: QGraphicsItem move influences other items in scene

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItem move influences other items in scene

    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:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent)
    3. {
    4. QGraphicsScene* scene = new QGraphicsScene(this);
    5. QGraphicsView* view = new QGraphicsView(this);
    6. view->setScene(scene);
    7. view->setMinimumSize(400, 200);
    8.  
    9. QGraphicsRectItem* item1 = new QGraphicsRectItem(0, 0, 50, 50);
    10. item1->setFlag(QGraphicsItem::ItemIsMovable, true);
    11. item1->setFlag(QGraphicsItem::ItemIsSelectable, true);
    12. QGraphicsRectItem* item2 = new QGraphicsRectItem(0, 0, 50, 50);
    13. item2->setFlags(item1->flags());
    14.  
    15. scene->addItem(item1);
    16. scene->addItem(item2);
    17.  
    18. setCentralWidget(view);
    19. }
    To copy to clipboard, switch view to plain text mode 

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

    Thank you for your help.

    Small compilable example attached.
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem move influences other items in scene

    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?
    Last edited by amleto; 27th November 2012 at 01:54.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem move influences other items in scene

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Problem with positioning a QGraphicsItem in the scene
    By qlands in forum Qt Programming
    Replies: 0
    Last Post: 1st October 2010, 11:05
  2. Overload QGraphicsItem Move in Scene
    By D Cell in forum Newbie
    Replies: 2
    Last Post: 16th March 2010, 05:14
  3. Can I copy a QGraphicsItem on my scene?
    By JesperWe in forum Qt Programming
    Replies: 3
    Last Post: 12th February 2010, 21:21
  4. add items into scene
    By Noxxik in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2009, 16:32
  5. Can't move Items in the scene
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2008, 09:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.