Results 1 to 2 of 2

Thread: Question regarding mouse event propogation re: QWidgets embedded in QGraphicsScene

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Question regarding mouse event propogation re: QWidgets embedded in QGraphicsScene

    I just started looking at Qt recently and haven't found any answer in the forums so far.

    I'm writing an app which is a context sensitive mix of normal mouse operations + mouse gestures.

    To handle this, I override the respective mouse*Events along the propogation chain:

    Qt Code:
    1. void MyGraphicsView::mousePressEvent(QMouseEvent *e) {
    2. QGraphicsView::mousePressEvent(e); // propogate to QGraphicsScene
    3.  
    4. // start monitoring mouse movement to check if it's a gesture
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
    2. if (ofInterestAtThisTime) {
    3. QGraphicsScene::mousePressEvent(e); // propogate to QGraphicsItems
    4. } else {
    5. // ignore it
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *e) {
    2. if (ofInterestAtThisTime) {
    3. // do something with the event
    4. } else {
    5. // ignore it
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now I'd like to embed QWidgets into the QGraphicsScene as well and have similar behaviour as above: the QGraphicsScene may (or may not) forward mouse*Events to QWidget but QWidget should be able to decide to act or not.

    Q1: How do I do intercept the mouse*Events for a QWidget embedded in a QGraphicsScene? I tried this but it seems the handler was being called regardless of QGraphicsScene:

    Qt Code:
    1. class MyWidget : public QWidget {
    2. void mouse*Event(QMouseEvent *event) {
    3. // this code called regardless of QGraphicsScene handling?
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 

    Q2: Should I be using QGraphicsProxyWidget instead? If so, how do I put the widget into the scene?

    Qt Code:
    1. class MyEmbeddedWidget : public QGraphicsProxyWidget {
    2. void mouse*Event(QGraphicsSceneMouseEvent *event) {
    3. // implement custom handler here
    4. }
    5. }
    6.  
    7. class MyWidget : public QWidget {
    8. // widget stuff
    9. }
    10. ...
    11. MyGraphicsScene *scene = new MyGraphicsScene();
    12. QGraphicsProxyWidget *proxy = new MyEmbeddedWidget();
    13. MyWidget *widget = new MyWidget();
    14. proxy->setWidget(widget);
    15.  
    16. // Q: How do I embed widget into scene now? Use scene::addWidget?
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question regarding mouse event propogation re: QWidgets embedded in QGraphicsScen

    Ok, so I found the solution which is:
    1. Subclass QGraphicsProxyWidget
    2. use setWidget to attach custom widget to the QGraphicsProxyWidget
    3. Then you can respond to mouse clicks in the QGraphicsProxyWidget subclass and everything stays in the QGraphicsView framework (ie. QGraphicsSceneMouseEvents instead of QMouseEvents).

    For those who are interested, check out the Embedded Dialogs demo (which I didn't see previously)
    http://qt.nokia.com/doc/4.5/demos-embeddeddialogs.html

Similar Threads

  1. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 11:28

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.