Results 1 to 5 of 5

Thread: Help programming dragging objects inside a QGraphicsScene

  1. #1
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Help programming dragging objects inside a QGraphicsScene

    Hi everybody! I need a little example about moving a picture or any object inside a QGraphicsScene Widget. The object or picture must be a GraphicsItem.

    I tried with the examples of Qt4 Demo but I am looking for something simple.
    I am developing an application where I use QGraphicsView, QGraphicsScene and QGraphicsItem, then I need to drag and drop the QGraphicsItem around the scene, but I don't find the way to do that.

    Regards!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help programming dragging objects inside a QGraphicsScene

    You can make a QGraphicsItem movable like this:
    Qt Code:
    1. item->setFlag(QGraphicsItem::ItemIsMovable, true);
    To copy to clipboard, switch view to plain text mode 
    Also, you can restrict how and where an item can be moved by reimplementing QGraphicsItem::itemChange() (notice the example in docs).
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    drake1983 (10th July 2007)

  4. #3
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help programming dragging objects inside a QGraphicsScene

    Thanks so much! It works!!

    Now, I have a little problem, I have two GraphicsItems in the scene, when I move one of them, the other item also moves to the opposite direction.

    I attached my program above.
    Attached Files Attached Files

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Help programming dragging objects inside a QGraphicsScene

    Quote Originally Posted by drake1983 View Post
    I have two GraphicsItems in the scene, when I move one of them, the other item also moves to the opposite direction.
    This is because no area is set for the scene, see QGraphicsScene::sceneRect(). If no scene rect is set, the scene grows when items are added and/or moved. The view can also be made to align scene differently and a resize anchor may be set.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    drake1983 (11th July 2007)

  7. #5
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help programming dragging objects inside a QGraphicsScene

    Thanks! Now everything works fine!!!

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.