QScrollArea是否对wheelEvent以及儿童占用的空间有反应?

时间:2019-01-09 00:07:34

标签: python pyqt pyqt5 qscrollarea

考虑以下示例:

#!/usr/bin/env python

import sys,os
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import Qt

class MainWindow(QtWidgets.QMainWindow):

    class ScrollAreaWheel(QtWidgets.QScrollArea): # SO:9475772
        def __init__(self, parent=None):
            super(MainWindow.ScrollAreaWheel, self).__init__(parent)
            self.parent = parent
        def wheelEvent(self, event):
            print("wheelEvent", event.angleDelta().y())

    def __init__(self):
        #~ self.do_init = QtCore.QEvent.registerEventType()
        QtWidgets.QMainWindow.__init__(self)
        self.setMinimumWidth(1000)
        self.setMinimumHeight(400)
        self.frame1 = QtWidgets.QFrame(self)
        self.frame1.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame1layout = QtWidgets.QGridLayout(self.frame1)
        self.frame1layout.setSpacing(0);
        self.frame1layout.setContentsMargins(0,0,0,0);

        self.frame1widget = QtWidgets.QWidget()
        self.frame1widget.setLayout(QtWidgets.QGridLayout())
        self.frame1layout.addWidget(self.frame1widget)

        self.frame1scroll = MainWindow.ScrollAreaWheel(self) #QtWidgets.QScrollArea()
        self.frame1scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.frame1scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.frame1widget.layout().addWidget(self.frame1scroll, 0, 0) #, Qt.AlignCenter)

        #self.frame1scrolllayout = QtWidgets.QHBoxLayout(self.frame1scroll)
        self.frame1scrolllayout = QtWidgets.QGridLayout(self.frame1scroll)
        self.frame1scroll.setWidget(self.frame1scrolllayout.widget())
        self.frame1scroll.setWidgetResizable(True)
        self.frame1scroll.setAlignment(Qt.AlignCenter)

        self.frame1label = QtWidgets.QLabel()
        self.frame1scrolllayout.addWidget(self.frame1label, 0, 0, Qt.AlignCenter) ##

        pixmap = QtGui.QPixmap(200, 100)
        pixmap.fill(Qt.red)
        self.frame1label.setPixmap(pixmap)

        self.frame2 = QtWidgets.QFrame(self)
        self.frame2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame2layout = QtWidgets.QHBoxLayout(self.frame2)
        self.frame2layout.setSpacing(0);
        self.frame2layout.setContentsMargins(0,0,0,0);
        self.frame2scroll = QtWidgets.QScrollArea(self)
        self.frame2scroll.setWidgetResizable(True)
        self.frame2widget = QtWidgets.QWidget()
        self.frame2widget.setLayout(QtWidgets.QGridLayout())
        self.frame2scroll.setWidget(self.frame2widget)
        self.frame2layout.addWidget(self.frame2scroll)

        self.mainwid = QtWidgets.QWidget()
        self.mainwid.setLayout(QtWidgets.QGridLayout())
        self.setCentralWidget(self.mainwid)

        self.splitter1 = QtWidgets.QSplitter(Qt.Horizontal)
        self.splitter1.addWidget(self.frame1)
        self.splitter1.addWidget(self.frame2)
        self.splitter1.setSizes([600, 600]); # equal splitter at start
        self.mainwid.layout().addWidget(self.splitter1)
        self.mainwid.layout().update()


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

它会生成此内容(Ubuntu 18.04):

pyqt5

我只想在左侧的QScrollArea上使用鼠标滚轮,为此我做了一个单独的类。但是,仅当我在红色框外时才会触发其wheelEvent,而不是将鼠标悬停在其上时才触发。即使鼠标悬停在子标签(红色框)上,如何使ScrollAreaWheel.wheelEvent触发?

1 个答案:

答案 0 :(得分:1)

您是 fmt.Println("Success", reflect.TypeOf(result.Reservations[0].Instances[0].Architecture)) Success *string fmt.Println("Success", result.Reservations[0].Instances[0].Architecture) Success 0xc0001ae4a8 放在QLabel的顶部,而不是放在里面,在视觉上是一样的,但在事件级别却不是。

QScrollArea