来自 child 的 parent 中的 PyQT4 WheelEvent

PyQT4 WheelEvent in parent from child

您好,我的问题与此有关:PyQT4 WheelEvent

from PyQt4.QtGui import *
from PyQt4.QtCore import *

import sys


class Main(QWidget):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout = QHBoxLayout(self)
        layout.addWidget(Scroll(self))


class Scroll(QScrollArea):

    def __init__(self, parent=None):
        super(Scroll, self).__init__(parent)
        self.parent = parent

    def wheelEvent(self, event):
        super(Scroll, self).wheelEvent(event)
        print "wheelEvent", event.delta()

        newHeight = self.parent.geometry().height() - event.delta()
        width     = self.parent.geometry().width()
        self.parent.resize(width, newHeight)

app = QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

我的 Scroll class 是在 Qt Designer 中创建的,我无法访问它

是否有机会访问 Scroll class 但在 Main class 中的 PyQT WheelEvent?

使用 event-filter:

class Main(QWidget):
    def __init__(self, parent=None):
        ...            
        self.scroll.installEventFilter(self)

    def eventFilter(self, source, event):
        if event.type() == QEvent.Wheel and source is self.scroll:
            print "wheelEvent", event.delta()
            newHeight = self.geometry().height() - event.delta()
            width = self.geometry().width()
            self.resize(width, newHeight)
            # return True to consume the event
            return False
        return super(Main, self).eventFilter(source, event)