Results 1 to 2 of 2

Thread: QGraphicsItem scenePos() broken after aborting previous item drag

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question QGraphicsItem scenePos() broken after aborting previous item drag

    We're trying to abort an item drag when the user hits the ESC key before releasing the mouse button. We do that within the item's keyPressEvent() method by momentarily disabling the item. (See code, below).

    That basically works, but it causes a problem. During the subsequent move-drag operation of a different QGraphicsItem, in the processing of mouseMoveEvents, the item is "warped" to near the origin of the scene, and QGraphicsItem::scenePos() is, in fact, returning those sorts of coordinates: it's returning the difference of the drag mouse position from the mouse press position which started the item drag.

    This is with Qt version Qt 4.3.3, both Solaris and Windows XP. Here's a excerpt (and minor simplification) of our code: (1) keyPressEvent, and (2) mouseMoveEvent:

    Qt Code:
    1. // virtual from QGraphicsItem
    2. void SimObjGfxItem::keyPressEvent (QKeyEvent* evt)
    3. {
    4. const QGraphicsItem* mGrabItem = scene()->mouseGrabberItem();
    5. const bool mouseGrabActive = (this == mGrabItem);
    6. const bool isEscKey (evt->key() == Qt::Key_Escape);
    7.  
    8. if (mouseGrabActive && isEscKey)
    9. {
    10. // Schedule restoration of Pre-Move Positions
    11. _restorePreMovePosOnNextFocusOut = true;
    12.  
    13. // DROP THE MOUSE GRAB (by momentarily becomming disabled).
    14. // This works, but it causes scenePos() to return strange results
    15. // during mouseMoveEvents on the subsequent drag-move of a different
    16. // QGraphicsItem. (See below).
    17. //
    18. setEnabled (false);
    19. qApp->processEvents(); // (same behavior with or without this).
    20. setEnabled (true);
    21. setSelected (true);
    22.  
    23. return;
    24. //------->>
    25. }
    26.  
    27. // call base class method
    28. QGraphicsItem::keyPressEvent (evt);
    29. }
    30.  
    31. //----------------
    32.  
    33. // virtual from QGraphicsItem
    34. void SimObjGfxItem::mouseMoveEvent (QGraphicsSceneMouseEvent* evt)
    35. {
    36. // Call base class method
    37. QGraphicsItem::mouseMoveEvent (evt);
    38.  
    39. ... ... ...
    40.  
    41. // NOTE: On the QGraphicsItem drag (only) immediately after aborting a
    42. // drag of another item with the ESC key -- see keyPressEvent, above --
    43. // QGraphicsItem::scenePos() returns instead coordinates relative to the
    44. // starting position of the QGraphicsItem drag -- i.e. on the first
    45. // several moves, near (0,0).
    46.  
    47. const QPointF objScenePos (scenePos());
    48. const double sceneX (objScenePos.x());
    49. const double sceneY (objScenePos.y());
    50.  
    51. ... ... ...
    52. std::cout << ...
    53. << " scene (" << sceneX << "," << sceneY << ")"
    54. << ... << std::endl;
    55. ... ... ...
    56. }
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance,
    Phil Weinstein, CADSWES,
    University of Colorado at Boulder, USA
    Last edited by philw; 24th February 2009 at 19:31.

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.