Anthony
Anthony

Reputation: 8788

Qt: How to click and drag in a QGraphicsView to rubber band select items that accept mouse clicks?

According to the QGraphicsView docs, dragMode's behavior "only affects mouse clicks that are not handled by any item." It then says "You can define a custom behavior by creating a subclass of QGraphicsView."

In my case I'd like clicks on an item that accepts mouse clicks to call the item's mouse clicks as normal. And I'd like clicks not on those items to start a rubber band drag (also as normal). However, I'd like to be able to ctrl-click the view, and have that start a rubber band drag selection rather than call the item's mouse event methods.

I know how to assess whether or not ctrl is associated with a mouse click event:

if (event->modifiers().testFlag(Qt::ControlModifier))

However, I don't know what to do with this condition, or where exactly to put it. Should I put it in QGraphicsItem, QGraphicsScene, or QGraphicsView? QGraphicsView seems most likely, as the docs somewhat hint.

Upvotes: 2

Views: 2801

Answers (1)

waseq
waseq

Reputation: 36

You need to put the condition to QGraphicsItem like this:

if (event->modifiers().testFlag(Qt::ControlModifier))
{event->setAccepted(false); return;}

Now You can handle the event in Your QGraphicsView.

Upvotes: 2

Related Questions