简体   繁体   中英

capture a copy of other QWidget events?

I am working on a widget that needs to update itself when another widget it is matched with moves or is resized. Currently, I have the other widget do its own resizeEvent() and moveEvent() and inside that it emits a signal that my widget connects to.

However, I do not like this setup. Say later on I want to have my other widget do a different thing with its resizeEvent() .

Is there a way for widget A (from widget A only ) to be informed when widget B 's resizeEvent() or moveEvent() is fired?

You could create a Helper class that is responsible for monitoring, so you do not need to overwrite the classes.

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Helper(QObject):
    def setWidgets(self, emmiter, receiver):
        emmiter.installEventFilter(self)
        self.emmiter = emmiter
        self.receiver = receiver

    def eventFilter(self, obj, event):
        if obj == self.emmiter:
            if event.type() == QEvent.Resize: 
                self.receiver.resize(self.emmiter.size())
            elif event.type() == QEvent.Move:
                self.receiver.move(event.pos())
        return QObject.eventFilter(self, obj, event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    helper = Helper()
    w1 = QWidget()
    w1.setWindowTitle("emmiter")
    w2 = QWidget()
    helper.setWidgets(w1, w2)
    w1.show()
    w2.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM