4

I am developing an app.I would like there to be an option to zoom in and out. to do this I would like to have the program get when the control key is clicked and the mouse is being scrolled at the same time, and zoom accordingly(scroll up for zoom in, and scroll down for zoom out) I've been doing a lot of searching and reading,but only found ways to get a control press and a mouse click-event, not a mouse scroll-event. I also found a way to get a mouse scroll but couldn't get it to work with just control press. - and to ignore all other presses.

Could anyone suggest something to help me???

1 Answer 1

5

It depends a bit on the structure of your application. One way to get scroll-events is to add a wheelEvent handler to your Widget

def wheelEvent(self, QWheelEvent):
    modifiers = QtGui.QApplication.keyboardModifiers()
    if modifiers == QtCore.Qt.ControlModifier:
        # do your processing

Another approach could be to install an eventFilter in the component where you want to intercept the scroll-events by

component.viewport().installEventFilter(self)  

(maybe you have to install the event filter on the component itself iso on the viewport).

and self has an eventFilter function like

 def eventFilter(self, qobject, event):
        if (event.type() == QtCore.QEvent.Wheel) :
            modifiers = QtGui.QApplication.keyboardModifiers()
            if modifiers == QtCore.Qt.ControlModifier:
                #do some scaling stuff
                return True
            return False
        else:
            # standard event processing
            return False

Hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but how could I know if the wheel event is scroll up or down?
the sign of event.delta() (see doc.qt.io/qt-4.8/qwheelevent.html)
which approach are you using? The wheelEvent handler or the eventFilter? With the former approach (and with the second one when the event filter is not installed correctly), it can happen that you only receive scroll-up events when at the top of your view and scroll-down events when you are at the bottom of your view. Can you confirm this behavior? Can you also indicate the type of your component and whether or not you are installing the event filter on the viewport?
Here is my code: def wheelEvent(self, event): modifiers = QtGui.QApplication.keyboardModifiers(); if modifiers == QtCore.Qt.ControlModifier: # here I zoom in; else: super(MainWindow, self).wheelEvent(event);
On which object did you define this function? and which (scrollable) views does it contain? Probably there is a scrollable container inside that is consuming the scroll-down events. If so, you'll have to switch to the second approach (using eventFilter)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.