-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds dragging for mouse, so by pressing middle mouse button dragging mode is being changed to scroll hand drag, basically it allows to navigate on the image without pressing scroll bars. To do that, View class had to be introduced which has been introduced to override some of QGraphicsView mouse events and to promote QGraphicsView widget to it.
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "view.h" | ||
|
||
View::View(QWidget *parent) | ||
: QGraphicsView(parent) | ||
{ | ||
} | ||
|
||
void View::mousePressEvent(QMouseEvent *event) | ||
{ | ||
switch (event->button()) { | ||
case Qt::LeftButton: { | ||
QGraphicsView::mousePressEvent(event); | ||
break; | ||
} | ||
case Qt::MiddleButton: { | ||
this->setDragMode(QGraphicsView::ScrollHandDrag); | ||
|
||
// after middle button has been pressed - send the mouse press event to base | ||
// class that holds this scene, since it will toggle on dragging on ScrollHandDrag | ||
QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, | ||
event->pos(), event->globalPosition(), Qt::MouseButton::LeftButton, | ||
Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier); | ||
|
||
QGraphicsView::mousePressEvent(pressEvent); | ||
break; | ||
} | ||
default: { | ||
QGraphicsView::mousePressEvent(event); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void View::mouseReleaseEvent(QMouseEvent *event) | ||
{ | ||
switch (event->button()) { | ||
case Qt::LeftButton: { | ||
break; | ||
} | ||
case Qt::MiddleButton: { | ||
this->setDragMode(QGraphicsView::NoDrag); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <QGraphicsView> | ||
#include <QMouseEvent> | ||
#include <QObject> | ||
|
||
class View : public QGraphicsView { | ||
Q_OBJECT | ||
|
||
public: | ||
View(QWidget *parent = nullptr); | ||
|
||
private slots: | ||
void mouseReleaseEvent(QMouseEvent *event); | ||
void mousePressEvent(QMouseEvent *event); | ||
}; |