PDA

View Full Version : Redirecting mouse events from a QGraphicsView to a QGraphicsScene



blooglet
12th May 2011, 10:16
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::mousePressEven t(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);
}
}
}


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?

wysota
18th May 2011, 00:01
The thing is it is the scene that does selection of items and not the view and the default behaviour of event handlers in QGraphicsView is to forward the event to the scene so basically you're trying to implement the already present behaviour of Graphics View. If you want to prevent items from being selected then override QGraphicsScene::mousePressEvent() and don't call the base class implementation. Or simply remove the selectable flag from your items temporarily.

blooglet
18th May 2011, 15:55
I tried doing that (see this related thread (http://www.qtcentre.org/threads/41156-Disabling-ItemIsSelectable-for-all-objects-in-the-graphics-scene?highlight=)), but I was told this:


Your view propagates mouse clicks directly to the items,therefore they become selected.Thats why disabling mouse events for the scene does not work.

wysota
18th May 2011, 16:07
You were told wrong. Here is the code of the view's mousePressEvent (with some irrelevant parts cut out):


void QGraphicsView::mousePressEvent(QMouseEvent *event)
{
Q_D(QGraphicsView);

// Store this event for replaying, finding deltas, and for
// scroll-dragging; even in non-interactive mode, scroll hand dragging is
// allowed, so we store the event at the very top of this function.
d->storeMouseEvent(event);
d->lastMouseEvent.setAccepted(false);

if (d->sceneInteractionAllowed) {
// Store some of the event's button-down data.
d->mousePressViewPoint = event->pos();
d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
d->mousePressScreenPoint = event->globalPos();
d->lastMouseMoveScenePoint = d->mousePressScenePoint;
d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
d->mousePressButton = event->button();

if (d->scene) {
// Convert and deliver the mouse event to the scene.
QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
mouseEvent.setWidget(viewport());
mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
mouseEvent.setScenePos(d->mousePressScenePoint);
mouseEvent.setScreenPos(d->mousePressScreenPoint);
mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
mouseEvent.setButtons(event->buttons());
mouseEvent.setButton(event->button());
mouseEvent.setModifiers(event->modifiers());
mouseEvent.setAccepted(false);
if (event->spontaneous())
qt_sendSpontaneousEvent(d->scene, &mouseEvent);
else
QApplication::sendEvent(d->scene, &mouseEvent);

// Update the original mouse event accepted state.
bool isAccepted = mouseEvent.isAccepted();
event->setAccepted(isAccepted);

// Update the last mouse event accepted state.
d->lastMouseEvent.setAccepted(isAccepted);

if (isAccepted)
return;
}
}
// cut out here
}

Have a look at lines 22-37.

The beauty of Open Source software...

blooglet
20th May 2011, 15:37
So the poster (http://www.qtcentre.org/threads/41156-Disabling-ItemIsSelectable-for-all-objects-in-the-graphics-scene) is wrong?

Then what is the correct way to prevent items from being selected in a QGraphicsScene if my graphics scene is in a state where I don't want the user to be able to select items? I override the default implementation of QGraphicsScene::mousePressEvent, QGraphicsScene::mouseMoveEvent and QGraphicsScene::mouseReleaseEvent when the scene is in a non-select state and I don't call the base class implementation. Still, I'm able to select items that have QGraphicsItem::ItemIsSelectable enabled by double-clicking them. How do I properly prevent items from being selected in a non-select state?

wysota
20th May 2011, 18:52
So the poster (http://www.qtcentre.org/threads/41156-Disabling-ItemIsSelectable-for-all-objects-in-the-graphics-scene) is wrong?
You can judge that yourself based on the code I provided.


Then what is the correct way to prevent items from being selected in a QGraphicsScene if my graphics scene is in a state where I don't want the user to be able to select items?
The correct way is to remove the ItemIsSelectable flag from the items. Another good way is to not call the base class implementation of mouse events in the scene but then you lose not only selection but also other features such as focus and item moving.


I override the default implementation of QGraphicsScene::mousePressEvent, QGraphicsScene::mouseMoveEvent and QGraphicsScene::mouseReleaseEvent when the scene is in a non-select state and I don't call the base class implementation. Still, I'm able to select items that have QGraphicsItem::ItemIsSelectable enabled by double-clicking them.
Apparently you are doing something wrong. My guess is you didn't override mouseDoubleClickEvent.

#include <QtGui>

class GraphicsScene : public QGraphicsScene {
public:
GraphicsScene() : QGraphicsScene(){}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e) {

}
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e){}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
GraphicsScene scene;
QGraphicsView view;
view.setScene(&scene);
QGraphicsEllipseItem *item = scene.addEllipse(QRect(0,0,100,50), QPen(Qt::red), Qt::blue);
item->setFlag(QGraphicsItem::ItemIsSelectable);
view.show();
return app.exec();
}

blooglet
22nd May 2011, 15:26
My guess is you didn't override mouseDoubleClickEvent.
D'oh. The answer was staring me in the face the entire time. Thanks for helping me sort this out!

wysota
22nd May 2011, 17:01
Just remember this is not the proper way of solving this problem.