You need to log in to create posts and topics.

CircuitView. Scroll instead of scale in case of Keyboard modifier (Ctrl/Shift) is used.

Hi all!

I've used SketchUp web for some time and really like it's scrolling approach - moving two fingers on a trackpad while Shift is pressed leads to horizontal / vertical scroll. Depending of the movement direction.

I tried to somewhat recreate this for Circuit view. In a much simplier way - no movement direction detection . Just a wheel event + Ctrl/Shift modifier for vertical / horizontal scroll. And failed). I mean it was a quick attemtpt. I don't have QT development environment set up - no QT creator or any other QT-specific tool and sadly no QT skills. I'm just using a latest dev build.
I wanted to give it a quick try using google and common sense) no luck.

void CircuitView::wheelEvent( QWheelEvent* event )
{
    //  add scroll instead of scale when Cth or Shift is pressed during wheel event
    bool noModifier = event->modifiers() == Qt::KeyboardModifier::NoModifier;
    if (m_showScroll && !noModifier){
        if (event->modifiers() == Qt::KeyboardModifier::ControlModifier){
            self()->horizontalScrollBar()->wheelEvent(event);
            return;
        }
        if (event->modifiers() == Qt::KeyboardModifier::ShiftModifier){
            self()->verticalScrollBar()->wheelEvent(event);
            return;
        }
    }
    qreal scaleFactor = pow( 2.0, event->delta() / 700.0);
    scale( scaleFactor, scaleFactor );
    m_scale *= scaleFactor;
}

But, it seems that's not how it should be done. Build results with

error: ‘virtual void QScrollBar::wheelEvent(QWheelEvent*)’ is protected within this context

So, here are the questions:
1 - is it really a quick stuff to implement?
2 - is the point of modifications selected correctly?
3 - is the selected approach overall right?

Hi.

You can pan the circuit using the middle button.
Some trackpads in laptops have an actual middle button, if not it can be emulated I think.

About your questions:
1 - Not sure, maybe you can use a similar approach to the middle button implementation.
2 - Yes, I think 2 finger events in trackpads are delivered to CircuitView::wheelEvent, but maybe not in all systems.
3 - As you can see QScrollBar::wheelEvent(QWheelEvent*) is protected. Maybe you can use QScrollBar::setValue(int)

There are classic 2-finger gestures for Pan and Zoom, so I was wondered why 2 finger move makes zoom, and closing/expanding fingers do nothing. Shifts, Controls, Third mouse button are not intuitive to me, I'm using mac trackpad.  So let's fix it.
Some theory:
Pan Gestures (in Qt) are converted to Mouse Wheel messages which can have a y and also a x offset. This seems strange to me, as there is no common Mouse with a horizontal Wheel. Even more confusing, by MS definition Pan events are converted to WM_VSCROLL/WM_HSCROLL events.
with Qt 6 there is QInputEvent::deviceType() which returns ::Mouse or ::TouchPad, depending on what generated the event. I think we're out of luck with Qt 5 though.
The zoom gesture with two fingers are handled differently on Mac and Windows:
On windows a wheel event is sent with “(event->modifiers() & Qt::ControlModifier) == true”. On the mac not.
So, if(event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier)) in CircuitView::wheelEvent
we must do a Windows Zoom, else Mac Pan with QGraphicsView::wheelEvent(event);
Now implement Mac Zoom: we have to listen to a QEvent::NativeGesture - thats not called on Windows:

bool CircuitView::viewportEvent ( QEvent * event ){
switch (event->type()){
case QEvent::NativeGesture:
auto nge = dynamic_cast<QNativeGestureEvent*>(event);
if (nge){
if(nge->gestureType() == Qt::ZoomNativeGesture){
qreal scaleFactor = pow( 2.0, nge->value());

That's all, folks. Enjoy classic Pan/Zoom gestures on Mac. Full code is here: 44608e4
BTW: You still can use old-style zoom with Shift pressed.

Cool stuff )

Thanks, I will have a look as soon as possible.
In any case I don't want to zoom with Shift pressed.

Quote from FeeLynn on September 3, 2024, 11:13 am

There are classic 2-finger gestures for Pan and Zoom, so I was wondered why 2 finger move makes zoom, and closing/expanding fingers do nothing. Shifts, Controls, Third mouse button are not intuitive to me, I'm using mac trackpad.  So let's fix it.
Some theory:
Pan Gestures (in Qt) are converted to Mouse Wheel messages which can have a y and also a x offset. This seems strange to me, as there is no common Mouse with a horizontal Wheel. Even more confusing, by MS definition Pan events are converted to WM_VSCROLL/WM_HSCROLL events.
with Qt 6 there is QInputEvent::deviceType() which returns ::Mouse or ::TouchPad, depending on what generated the event. I think we're out of luck with Qt 5 though.
The zoom gesture with two fingers are handled differently on Mac and Windows:
On windows a wheel event is sent with “(event->modifiers() & Qt::ControlModifier) == true”. On the mac not.
So, if(event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier)) in CircuitView::wheelEvent
we must do a Windows Zoom, else Mac Pan with QGraphicsView::wheelEvent(event);
Now implement Mac Zoom: we have to listen to a QEvent::NativeGesture - thats not called on Windows:

bool CircuitView::viewportEvent ( QEvent * event ){
switch (event->type()){
case QEvent::NativeGesture:
auto nge = dynamic_cast<QNativeGestureEvent*>(event);
if (nge){
if(nge->gestureType() == Qt::ZoomNativeGesture){
qreal scaleFactor = pow( 2.0, nge->value());

That's all, folks. Enjoy classic Pan/Zoom gestures on Mac. Full code is here: 44608e4
BTW: You still can use old-style zoom with Shift pressed.

 

Well.. let's analyze this issue:

This discussion was about scrolling, not zooming.
Zooming in a track-pad with two fingers is working.
So I don't see the reason for this code.

Also if you copy-paste the code and most of your post from the internet, please add a link to the original post:
https://stackoverflow.com/questions/70037430/qt-how-to-implement-panning-zooming-in-qgraphicsscene-with-two-finger-gestures/70263559#70263559

 

This discussion was about scrolling, not zooming. - Exactly! This way we have scrolling AND zooming both work as intended. I tested it, works perfect.
Zooming in a track-pad with two fingers is working. - No. I can't tell this is "working". Let's call it "exists". 2-finger-move gesture must do Pan, not Zoom. I can't Pan with Third mouse button because of no mouse. 
So I don't see the reason for this code. - You're the owner - You choose. 
Also if you copy-paste the code... The Stack Overflow terms of service indicate that content, including code, can be copied for personal, non-commercial use only and is subject to the copyleft Creative Commons Attribution-Share Alike 4.0 International (CC BY-SA 4.0) license. At least in the US, the legal doctrine of Fair Use does not apply to embedding excerpts even of copyrighted works into source code.

I saw there was no solution suggested in three months, so I posted one. Compiled and Tested. Am I wrong?

I saw there was no solution suggested in three months, so I posted one. Compiled and Tested. Am I wrong?

Maybe it works for you, I have no idea.
But this not a solution.

Your code has nothing about scrolling, only about zoom, and has this effect:
- Does nothing in Windows laptop: zoom is already working and still no scroll.
- Breaks zoom in Linux laptop: zoom was working and still no scroll.
- Breaks zoom with mouse wheel in Windows, Linux and Mac (now Shift key is needed).

I guess in your case scrolling was working and zoom was not working, but I have no idea because you didn't explain.

 

I managed to make scroll to work with your code at least in Windows by restoring track-pad configuration to default values.
I will do some more investigation in Linux, maybe something similar is happening.

I can't find a combination that don't break zooming with Mouse middle button and works for all systems.
But I added the option to scroll with Shift+Left button for trackpads that don't have middle button or are configured to don't use it.