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.
void MyCustomGraphicsViewImplementation
::mousePressEvent(QMouseEvent *event
) { MyCustomGraphicsSceneImplementation* theScene = dynamic_cast<MyCustomGraphicsSceneImplementation*>(this->scene());
if (theScene) {
/* Is our scene in Select mode? Then we'll follow the default implementation
* of mousePressEvent. */
if (theScene->getSceneMode() == MyCustomGraphicsSceneImplementation::Select) {
}
/* If we're in another scene mode, just send out the mouse event to the graphics scene without selecting anything */
else {
QMouseEvent gsEvent
(QEvent::MouseButtonPress, event
->pos
(), Qt
::LeftButton, Qt
::LeftButton, Qt
::NoModifier);
}
}
}
void MyCustomGraphicsViewImplementation::mousePressEvent(QMouseEvent *event) {
MyCustomGraphicsSceneImplementation* theScene = dynamic_cast<MyCustomGraphicsSceneImplementation*>(this->scene());
if (theScene) {
/* Is our scene in Select mode? Then we'll follow the default implementation
* of mousePressEvent. */
if (theScene->getSceneMode() == MyCustomGraphicsSceneImplementation::Select) {
QGraphicsView::mousePressEvent(event);
}
/* If we're in another scene mode, just send out the mouse event to the graphics scene without selecting anything */
else {
QMouseEvent gsEvent(QEvent::MouseButtonPress, event->pos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(theScene, &gsEvent);
}
}
}
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?
Bookmarks