Solved How to implement Panning/Zooming in QGraphicsScene with two finger gestures on laptop trackpad (win/mac)?
-
I have a Qt 6.2 Application (Windows/Mac) using QGraphicsScene and want to use 2 fingers on the touch pad of my laptop for panning - as many other applications do.
Zooming in/out works fine, but using 2 fingers for panning always results in zoom out.
I found a number of questions and some fragmentary samples. But no half way complete example:
https://stackoverflow.com/questions/8089838/finger-scrolling-in-qgraphicsview-in-qt https://forum.qt.io/topic/119221/how-to-listen-to-qtouchevent-originating-from-a-precision-touch-pad https://doc.qt.io/qt-5/gestures-overview.html]([link url](link url))
What is the right way to do this?
What i tried so far (on Windows):
MyGraphicsView::MyGraphicsView(...) : QGraphicsView() { viewport()->setAttribute(Qt::WA_AcceptTouchEvents); ... bool MyGraphicsView::viewportEvent ( QEvent * event ) { switch (event->type()) { case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: { // never called }
MyDocWin::MyDocWin(...) : CMDIAppDocWin(), { setAttribute(Qt::WA_AcceptTouchEvents); ... bool MyDocWin::event(QEvent *event) { switch (event->type()) { case QEvent::TouchBegin: case QEvent::TouchUpdate: case QEvent::TouchEnd: { // never called } ...
std::vector<Qt::GestureType> sgGestureTypes = {Qt::TapGesture, Qt::TapAndHoldGesture,Qt::PanGesture ,Qt::PinchGesture ,Qt::SwipeGesture }; MyGraphicsView::MyGraphicsView(...) : QGraphicsView() { for(Qt::GestureType gesture : sgGestureTypes) grabGesture(gesture); ... bool MyGraphicsView::event(QEvent *event) { switch (event->type()) { case QEvent::Gesture: case QEvent::NativeGesture: // never called
4) [MyDocWin::MyDocWin(...) : CMDIAppDocWin(), { for (Qt::GestureType gesture : sgGestureTypes) grabGesture(gesture); ... bool MyDocWin::event(QEvent *event) { switch (event->type()) { case QEvent::Gesture: case QEvent::NativeGesture: // never called]([link url](link url))
I already posted this on SO: https://stackoverflow.com/questions/70037430/how-to-pan-in-qgraphicsscene-with-two-fingers-on-laptop-trackpad
-
I finaly found out how to handle this. See the SO post at https://stackoverflow.com/questions/70037430/qt-how-to-implement-panning-in-qgraphicsscene-with-two-fingers-on-laptop-trackp/70263559#70263559
-
Touchpad gesture support for Wayland has landed in the Qt framework. This will be released as Qt 6.2.
Touchpad gesture support for X server on Qt framework has been submitted for review.
if you use 6.2 on Wayland, your code may work.
https://bill.harding.blog/2021/06/06/linux-touchpad-like-macbook-update-touchpad-gestures-land-to-qt-gimp-and-x-server/ -
I finaly found out how to handle this. See the SO post at https://stackoverflow.com/questions/70037430/qt-how-to-implement-panning-in-qgraphicsscene-with-two-fingers-on-laptop-trackp/70263559#70263559