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:
QGraphicsView::mousePressEvent(e
);
// propogate to QGraphicsScene
// start monitoring mouse movement to check if it's a gesture
}
void MyGraphicsView::mousePressEvent(QMouseEvent *e) {
QGraphicsView::mousePressEvent(e); // propogate to QGraphicsScene
// start monitoring mouse movement to check if it's a gesture
}
To copy to clipboard, switch view to plain text mode
if (ofInterestAtThisTime) {
} else {
// ignore it
}
}
void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *e) {
if (ofInterestAtThisTime) {
QGraphicsScene::mousePressEvent(e); // propogate to QGraphicsItems
} else {
// ignore it
}
}
To copy to clipboard, switch view to plain text mode
if (ofInterestAtThisTime) {
// do something with the event
} else {
// ignore it
}
}
void MyGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *e) {
if (ofInterestAtThisTime) {
// do something with the event
} else {
// ignore it
}
}
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:
// this code called regardless of QGraphicsScene handling?
}
}
class MyWidget : public QWidget {
void mouse*Event(QMouseEvent *event) {
// this code called regardless of QGraphicsScene handling?
}
}
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?
class MyEmbeddedWidget : public QGraphicsProxyWidget {
// implement custom handler here
}
}
// 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?
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?
To copy to clipboard, switch view to plain text mode
Bookmarks