Hi,

Trying to exploit the built-in Qt functionality regarding drag modes
(rubber-band, scroll-hand) and associated behavior in QGraphicsView, I
found a single line in QGraphicsView.setDragMode() that prevented my
beautiful and intuitive implementation to work and which I believe may
have been a bug.

Please find attached a patch and more information within.

Thanks.
From efc62fceaa94590b1cdaf0a87df705dc43f03f88 Mon Sep 17 00:00:00 2001
From: Kernc <kernc...@gmail.com>
Date: Wed, 23 Sep 2015 11:14:59 +0200
Subject: [PATCH] Unset QGraphicsView handScrolling when drag mode is reset
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Without this change, the user is unable to use the built-in
rubber-band/scroll-hand drag functionallity, each bound to a different
mouse key. E.g. The following code, in Python pseudo-code, did not
work before the patch:

    class GraphicsView(QGraphicsView):
        def __init__(self):
            # Rubber-band drag mode by default
            self.setDragMode(QGraphicsView.RubberBandDrag)

        def mousePressEvent(self, event):
            if event.button() == Qt.RightButton:
                # On right mouse button click, set the mode to scrolling
                self.setDragMode(QGraphicsView.ScrollHandDrag)
                # and forge a left-click mouse event
                event = QMouseEvent(event.type(),
                                    event.localPos(),
                                    Qt.LeftButton,
                                    event.buttons() | Qt.LeftButton,
                                    event.modifiers())
            # Have the built-in handler do its job
            super().mousePressEvent(event)

        def mouseReleaseEvent(self, event):
            super().mouseReleaseEvent(self, event)
            # Without this patch, this would have only worked if one
            # was to call:
            #
            # ←    self.setDragMode(QGraphicsView.NoDrag)
            #
            # right here. Without it, the handScrolling property
            # remains set, and any further behavior, particularly
            # the moving of selected QGraphicsItems in the attached
            # scene, becomes and remains weird.

            # With this patch, the handScrolling is reset also when
            # setting any other mode.
            self.setDragMode(QGraphicsView.RubberBandDrag)
---
 src/widgets/graphicsview/qgraphicsview.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp
index ac8cd45..03b9ab5 100644
--- a/src/widgets/graphicsview/qgraphicsview.cpp
+++ b/src/widgets/graphicsview/qgraphicsview.cpp
@@ -1495,7 +1495,7 @@ void QGraphicsView::setDragMode(DragMode mode)
     // don't unset the handScrolling state. When enabling scrolling
     // again the mouseMoveEvent will automatically start scrolling,
     // without a mousePress
-    if (d->dragMode == ScrollHandDrag && mode == NoDrag && d->handScrolling)
+    if (d->dragMode == ScrollHandDrag && mode != ScrollHandDrag && d->handScrolling)
         d->handScrolling = false;
 
     d->dragMode = mode;
-- 
2.1.4

_______________________________________________
Development mailing list
Development@qt-project.org
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to