Results 1 to 2 of 2

Thread: Make QGraphicsItem not moveable in QGraphicsScene

  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Make QGraphicsItem not moveable in QGraphicsScene

    I'm trying to make the qgraphicsitem not moveable. I tried setting the flag when its being clicked and tried setting it when the item is created etc. I can call flags(); and it shows that ItemIsMovable is not in the flags list yet it is still able to be moved. I have noticed when I pop up a modeless dialog in my mainwindow.cpp then go back to the mainwindow without closing the dialog and try to move an item it then recognizes the flag and I can't move the item until I close the dialog. I'm not sure why qgraphicsscene is ignoring the flag. Can anyone please explain how I can get this to work?

    Here is some of my code stripped down to the relevant stuff.

    Qt Code:
    1. //GraphScene.h
    2. class GraphScene : public QGraphicsScene
    3. {
    4. private:
    5. protected:
    6. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    7. };
    8.  
    9. //GraphScene.cpp
    10. void GraphScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    11. {
    12.  
    13. x = qgraphicsitem_cast<QGraphicsItem*>(itemAt(event->scenePos(), QTransform()));
    14.  
    15. if (x)
    16. {
    17. x->setFlag(QGraphicsItem::ItemIsMovable,false);
    18. }
    19.  
    20. QGraphicsScene::mousePressEvent(event);
    21. }
    22.  
    23. //y.h
    24. class y : public QGraphicsObject
    25. {
    26. protected:
    27. void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
    28. };
    29.  
    30. //y.cpp
    31. y::y()
    32. {
    33. setFlags(ItemIsSelectable | ItemIsFocusable);
    34. this->setFlag(QGraphicsItem::ItemIsMovable,false);
    35. setAcceptHoverEvents(true);
    36. }
    37. void y::mousePressEvent(QGraphicsSceneMouseEvent *event)
    38. {
    39. QGraphicsItem::mousePressEvent(event);
    40. update();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Noroxus; 18th March 2015 at 02:34.

  2. #2
    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: Make QGraphicsItem not moveable in QGraphicsScene

    Please provide a minimum compilable example reproducing the problem.
    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. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Replies: 4
    Last Post: 23rd March 2011, 16:22
  3. Stopped widget in a moveable QGraphicsScene
    By jano_alex_es in forum Newbie
    Replies: 3
    Last Post: 4th February 2011, 10:05
  4. QGraphicsItem-QGraphicsScene
    By hgedek in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2007, 12:35
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

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.