From 43f87292f550752eeabb0a00c063e403013f926e Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Sun, 29 Nov 2026 18:07:41 +0100 Subject: [PATCH] Take the drag mode from the modifiers of the mouse press The view missed the Shift release when another widget had the focus, and the rubber band mode stayed on. Fixes #363 --- src/GraphicsView.cpp | 5 +++++ test/CMakeLists.txt | 1 + test/src/TestViewDragMode.cpp | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 test/src/TestViewDragMode.cpp diff --git a/src/GraphicsView.cpp b/src/GraphicsView.cpp index e0b4092a..51adad2d 100644 --- a/src/GraphicsView.cpp +++ b/src/GraphicsView.cpp @@ -480,6 +480,11 @@ void GraphicsView::keyReleaseEvent(QKeyEvent *event) void GraphicsView::mousePressEvent(QMouseEvent *event) { + // The view may miss the Shift release, for example when another widget has the focus. + // The modifiers of the press are always right. + bool const shift = (event->modifiers() & Qt::ShiftModifier) != 0; + setDragMode(shift ? QGraphicsView::RubberBandDrag : QGraphicsView::ScrollHandDrag); + QGraphicsView::mousePressEvent(event); if (event->button() == Qt::LeftButton) { _clickPos = mapToScene(event->pos()); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 921d02c3..8a36fd1b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable(test_nodes src/TestZoomFeatures.cpp src/TestLoopDetection.cpp src/TestEmbeddedWidgetKeys.cpp + src/TestViewDragMode.cpp include/ApplicationSetup.hpp include/TestGraphModel.hpp include/UITestHelper.hpp diff --git a/test/src/TestViewDragMode.cpp b/test/src/TestViewDragMode.cpp new file mode 100644 index 00000000..e9f84539 --- /dev/null +++ b/test/src/TestViewDragMode.cpp @@ -0,0 +1,38 @@ +#include "ApplicationSetup.hpp" +#include "UITestHelper.hpp" + +#include +#include +#include +#include + +#include + +#include + +using QtNodes::DataFlowGraphicsScene; +using QtNodes::DataFlowGraphModel; +using QtNodes::GraphicsView; +using QtNodes::NodeDelegateModelRegistry; + +TEST_CASE("A mouse press without Shift drags the view even if the Shift release was not seen", + "[ui][drag-mode]") +{ + auto app = applicationSetup(); + + DataFlowGraphModel model(std::make_shared()); + DataFlowGraphicsScene scene(model); + GraphicsView view(&scene); + view.resize(800, 600); + view.show(); + REQUIRE(QTest::qWaitForWindowExposed(&view)); + view.setFocus(); + + QTest::keyPress(&view, Qt::Key_Shift); + REQUIRE(view.dragMode() == QGraphicsView::RubberBandDrag); + + // The release goes to another widget, so the view never sees it. + UITestHelper::simulateMousePress(&view, QPointF(0, 0)); + CHECK(view.dragMode() == QGraphicsView::ScrollHandDrag); + UITestHelper::simulateMouseRelease(&view, QPointF(0, 0)); +}