1

Is it possible to "lock" selection in Qt graphics view, to make clicks noneffective?

My scene can currently be in different states, and in some states selection should not change even if click happens.

AkiRoss
  • 11,745
  • 6
  • 59
  • 86
  • 1
    I answered, and then realised it may not be what you want. So to clarify, do want to lock the view (i.e. any clicking is ignored), or just ignore selection changes? – cmannett85 Feb 25 '13 at 23:29
  • I'd like to prevent any selection change *by user interaction*, but your solution is interesting anyway. I just hoped for already existing methods instead of subclassing. Just out of curiosity, how would you prevent any selection change caused by user interaction? – AkiRoss Feb 26 '13 at 08:11
  • 1
    The method I have given stops selection change by user interaction. You can stop selection changes in general quite easily (using `item->setFlag( ItemIsSelectable, false )`), but doing it just from interaction through the views gets complicated if you want retain panning or drag selection, because you will need to differentiate between clicks for selection and clicks that are beginning a drag. – cmannett85 Feb 26 '13 at 08:32
  • Yes, I noticed that flaw in your solution. Since I'm using custom items (subclassing QGraphicsViewItem), I was thinking about a global (static) flag which could operate directly on all the items using `setFlag(ItemIsSelectable, false)` as you proposed, but I hoped for something quicker operating on view or scene objects. – AkiRoss Feb 26 '13 at 08:36
  • The problem with `setFlag(ItemIsSelectable, false)` is that it cannot differentiate between user interaction and a programmably set selection command. – cmannett85 Feb 26 '13 at 08:52
  • Yes, but in your solution is it necessary to differentiate between clicks anyway (some clicks are selecting, other are not), so it's about the same effort, isn't it? – AkiRoss Feb 26 '13 at 12:59
  • 1
    Take a look at my edit, this seems better. – cmannett85 Feb 26 '13 at 13:28

1 Answers1

4

Reimplement the mousePressEvent(..) in the views connected to the scene to ignore events.

void MyView::mousePressEvent(QMouseEvent* event)
{
    if ( ignoreClickFlag && ( dragMode() == QGraphicsView::RubberBandDrag ||
                              items( event->pos() ).size() ) ) {
        event->accept();
        return;
    }

    QGraphicsView::mousePressEvent( event );
}

MyView::ignoreClickFlag is a just a bool member that you can set in a slot.

So if the ignore mode is on, and the view is in rubber band drag mode and/or there is a node under the cursor, the click will be ignored. Another way to put it is, clicking is ignored if the user can perform a selection drag and/or if there is a item under the cursor, but not if the view is in pan drag mode and there is no item under the cursor.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • This could work, but won't affect only selection: it will stop any click on the view to have any effect. – AkiRoss Feb 26 '13 at 08:38