Results 1 to 3 of 3

Thread: How to intercept mouse events in a scene

  1. #1
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to intercept mouse events in a scene

    I've subclassed the QGraphicsScene class into my own class, and tried to re-implement the method
    Qt Code:
    1. QGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    To copy to clipboard, switch view to plain text mode 
    so as I can do something interesting when the use clicks on the view (namely cause a QGraphicsItem object to appear). On running the code and clicking in the view though, this method is not called.

    Here are the relevant parts of code (as I see them), can anybody tell me what I'm missing?
    Qt Code:
    1. DiagramScene *scene = new DiagramScene();
    2. graphicsView = new QGraphicsView(scene);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class DiagramScene : public QGraphicsScene
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. DiagramScene(QObject *parent = 0);
    7.  
    8. protected:
    9. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    10. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
    11. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. DiagramScene::DiagramScene(QObject *parent)
    2. : QGraphicsScene(parent)
    3. {
    4. }
    5.  
    6. void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    7. {
    8. if (mouseEvent->button() != Qt::LeftButton)
    9. return;
    10. //do something here...
    11. }
    To copy to clipboard, switch view to plain text mode 

  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: How to intercept mouse events in a scene

    Use a qDebug() statement to see if your event handler gets called - it should. It might not behave as you expect it, but it should at least be called.

  3. #3
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to intercept mouse events in a scene

    The problem was that I was re-setting the scene in a different place, so as a new standard QGraphicsScene was being used. qDebug() statements are useful (easier to use than gdb , thanks.

Similar Threads

  1. Replies: 9
    Last Post: 22nd June 2008, 22:26
  2. Weird behaviour of mouse events and QMenu pop-ups
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 07:46
  3. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  4. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25
  5. Forwarding mouse events to another widget.
    By yogeshm02 in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2006, 13:25

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.