Results 1 to 8 of 8

Thread: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

  1. #1
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    I'd like to override my QGraphicsView's behavior when the QGraphicsScene inside it isn't in a certain state. I want to do this to prevent graphic items that have the ItemIsSelectable flag enabled from being selected.

    This is what I tried.

    Qt Code:
    1. void MyCustomGraphicsViewImplementation::mousePressEvent(QMouseEvent *event) {
    2. MyCustomGraphicsSceneImplementation* theScene = dynamic_cast<MyCustomGraphicsSceneImplementation*>(this->scene());
    3.  
    4. if (theScene) {
    5. /* Is our scene in Select mode? Then we'll follow the default implementation
    6.   * of mousePressEvent. */
    7. if (theScene->getSceneMode() == MyCustomGraphicsSceneImplementation::Select) {
    8. QGraphicsView::mousePressEvent(event);
    9. }
    10. /* If we're in another scene mode, just send out the mouse event to the graphics scene without selecting anything */
    11. else {
    12. QMouseEvent gsEvent(QEvent::MouseButtonPress, event->pos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    13. QApplication::sendEvent(theScene, &gsEvent);
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    This code compiles, but the mouse press event doesn't register correctly with the scene. How can I redirect the mouse event to the graphics scene without executing QGraphicsView::mousePressEvent?

  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: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    The thing is it is the scene that does selection of items and not the view and the default behaviour of event handlers in QGraphicsView is to forward the event to the scene so basically you're trying to implement the already present behaviour of Graphics View. If you want to prevent items from being selected then override QGraphicsScene::mousePressEvent() and don't call the base class implementation. Or simply remove the selectable flag from your items temporarily.
    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.


  3. #3
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    I tried doing that (see this related thread), but I was told this:

    Your view propagates mouse clicks directly to the items,therefore they become selected.Thats why disabling mouse events for the scene does not work.

  4. #4
    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: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    You were told wrong. Here is the code of the view's mousePressEvent (with some irrelevant parts cut out):

    Qt Code:
    1. void QGraphicsView::mousePressEvent(QMouseEvent *event)
    2. {
    3.  
    4. // Store this event for replaying, finding deltas, and for
    5. // scroll-dragging; even in non-interactive mode, scroll hand dragging is
    6. // allowed, so we store the event at the very top of this function.
    7. d->storeMouseEvent(event);
    8. d->lastMouseEvent.setAccepted(false);
    9.  
    10. if (d->sceneInteractionAllowed) {
    11. // Store some of the event's button-down data.
    12. d->mousePressViewPoint = event->pos();
    13. d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
    14. d->mousePressScreenPoint = event->globalPos();
    15. d->lastMouseMoveScenePoint = d->mousePressScenePoint;
    16. d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
    17. d->mousePressButton = event->button();
    18.  
    19. if (d->scene) {
    20. // Convert and deliver the mouse event to the scene.
    21. QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
    22. mouseEvent.setWidget(viewport());
    23. mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
    24. mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
    25. mouseEvent.setScenePos(d->mousePressScenePoint);
    26. mouseEvent.setScreenPos(d->mousePressScreenPoint);
    27. mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
    28. mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
    29. mouseEvent.setButtons(event->buttons());
    30. mouseEvent.setButton(event->button());
    31. mouseEvent.setModifiers(event->modifiers());
    32. mouseEvent.setAccepted(false);
    33. if (event->spontaneous())
    34. qt_sendSpontaneousEvent(d->scene, &mouseEvent);
    35. else
    36. QApplication::sendEvent(d->scene, &mouseEvent);
    37.  
    38. // Update the original mouse event accepted state.
    39. bool isAccepted = mouseEvent.isAccepted();
    40. event->setAccepted(isAccepted);
    41.  
    42. // Update the last mouse event accepted state.
    43. d->lastMouseEvent.setAccepted(isAccepted);
    44.  
    45. if (isAccepted)
    46. return;
    47. }
    48. }
    49. // cut out here
    50. }
    To copy to clipboard, switch view to plain text mode 

    Have a look at lines 22-37.

    The beauty of Open Source software...
    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.


  5. #5
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    So the poster is wrong?

    Then what is the correct way to prevent items from being selected in a QGraphicsScene if my graphics scene is in a state where I don't want the user to be able to select items? I override the default implementation of QGraphicsScene::mousePressEvent, QGraphicsScene::mouseMoveEvent and QGraphicsScene::mouseReleaseEvent when the scene is in a non-select state and I don't call the base class implementation. Still, I'm able to select items that have QGraphicsItem::ItemIsSelectable enabled by double-clicking them. How do I properly prevent items from being selected in a non-select state?

  6. #6
    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: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    Quote Originally Posted by blooglet View Post
    So the poster is wrong?
    You can judge that yourself based on the code I provided.

    Then what is the correct way to prevent items from being selected in a QGraphicsScene if my graphics scene is in a state where I don't want the user to be able to select items?
    The correct way is to remove the ItemIsSelectable flag from the items. Another good way is to not call the base class implementation of mouse events in the scene but then you lose not only selection but also other features such as focus and item moving.

    I override the default implementation of QGraphicsScene::mousePressEvent, QGraphicsScene::mouseMoveEvent and QGraphicsScene::mouseReleaseEvent when the scene is in a non-select state and I don't call the base class implementation. Still, I'm able to select items that have QGraphicsItem::ItemIsSelectable enabled by double-clicking them.
    Apparently you are doing something wrong. My guess is you didn't override mouseDoubleClickEvent.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class GraphicsScene : public QGraphicsScene {
    4. public:
    5. GraphicsScene() : QGraphicsScene(){}
    6. protected:
    7. void mousePressEvent(QGraphicsSceneMouseEvent *e) {
    8.  
    9. }
    10. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e){}
    11. };
    12.  
    13. int main(int argc, char **argv){
    14. QApplication app(argc, argv);
    15. GraphicsScene scene;
    16. view.setScene(&scene);
    17. QGraphicsEllipseItem *item = scene.addEllipse(QRect(0,0,100,50), QPen(Qt::red), Qt::blue);
    18. item->setFlag(QGraphicsItem::ItemIsSelectable);
    19. view.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    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.


  7. The following user says thank you to wysota for this useful post:

    blooglet (22nd May 2011)

  8. #7
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    Quote Originally Posted by wysota View Post
    My guess is you didn't override mouseDoubleClickEvent.
    D'oh. The answer was staring me in the face the entire time. Thanks for helping me sort this out!

  9. #8
    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: Redirecting mouse events from a QGraphicsView to a QGraphicsScene

    Just remember this is not the proper way of solving this 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 mouse events
    By high_flyer in forum Qt Programming
    Replies: 22
    Last Post: 29th May 2014, 09:03
  2. Replies: 5
    Last Post: 27th April 2010, 11:04
  3. how to connect events with signals in QGraphicsScene?
    By nataly in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 15:20
  4. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.