PDA

View Full Version : Question regarding mouse event propogation re: QWidgets embedded in QGraphicsScene



warthog
26th October 2009, 07:03
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:


void MyGraphicsView::mousePressEvent(QMouseEvent *e) {
QGraphicsView::mousePressEvent(e); // propogate to QGraphicsScene

// start monitoring mouse movement to check if it's a gesture
}


void MyGraphicsScene::mousePressEvent(QGraphicsSceneMou seEvent *e) {
if (ofInterestAtThisTime) {
QGraphicsScene::mousePressEvent(e); // propogate to QGraphicsItems
} else {
// ignore it
}
}


void MyGraphicsItem::mousePressEvent(QGraphicsSceneMous eEvent *e) {
if (ofInterestAtThisTime) {
// do something with the event
} else {
// ignore it
}
}

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:


class MyWidget : public QWidget {
void mouse*Event(QMouseEvent *event) {
// this code called regardless of QGraphicsScene handling?
}
}


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


class MyEmbeddedWidget : public QGraphicsProxyWidget {
void mouse*Event(QGraphicsSceneMouseEvent *event) {
// implement custom handler here
}
}

class MyWidget : public QWidget {
// widget stuff
}
...
MyGraphicsScene *scene = new MyGraphicsScene();
QGraphicsProxyWidget *proxy = new MyEmbeddedWidget();
MyWidget *widget = new MyWidget();
proxy->setWidget(widget);

// Q: How do I embed widget into scene now? Use scene::addWidget?

warthog
30th October 2009, 09:52
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