2
votes

I would like to simply disable mouse wheel scroll on QScrollArea, in order to scroll down only by clicking on right scrollbar, but I can't find any solution on the Internet.

app = QtGui.QApplication([])
sa = pg.QtGui.QScrollArea()
win = pg.GraphicsWindow()    
sa.setWidget(win)   

The problem is that I have a lot of graphs in my scroll area, and when I try to mousewheel on one of them, the page will scroll up or down together with the graph. I can't find a method to call on "sa" to disable mouse wheel scroll.

I found some posts discussing about install event filter but I can't understand how to use them in this case. For example, I tried to use this:

sa.viewport().installEventFilter(???)

but I really didn't understand what arguments to pass and how to check the event.

Thank you in advance if you can help me with this problem.

2

2 Answers

2
votes

I finally managed to resolve this, using this solution:

class Scroller(pg.QtGui.QScrollArea):
    def __init__(self):
        pg.QtGui.QScrollArea.__init__(self)
    def wheelEvent(self, ev):
        if ev.type() == QtCore.QEvent.Wheel:
            ev.ignore()

app = QtGui.QApplication([])
sa = Scroller() # <======
win = pg.GraphicsWindow()    
sa.setWidget(win) 
1
votes

You have the right idea. Event-filtering requires an object that inherits QObject to watch for the relevant events. Such objects have an eventFilter method which can be overriden to provide custom handing of all events for the watched object. If this method returns True for a given event, it will not be propagated any further. Usually the main-window is used to provide the event-filtering, like this:

import sys
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.scroll = QtGui.QScrollArea()
        self.widget = QtGui.QGraphicsView()
        self.widget.setFixedSize(600, 600)
        self.scroll.setWidget(self.widget)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.scroll)
        self.scroll.viewport().installEventFilter(self)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.Wheel and
            source is self.scroll.viewport()):
            return True
        return super(Window, self).eventFilter(source, event)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 400, 300)
    window.show()
    sys.exit(app.exec_())