凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

PyQt5 关联窗口关闭,主窗口关闭-子窗口跟着关闭

2019-08-27 python 5160

2.jpg

from PyQt5.QtWidgets import QWidget, QMainWindow
from PyQt5.QtCore import QObject,pyqtSignal

def Singletonfunc(cls):
    _instance = {}
    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    return _singleton

@Singletonfunc
class Signal(QObject):
    _mainClose = pyqtSignal(int)

# 关联窗口 - 主窗口
class RMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self._signal = Signal()

    def closeEvent(self, e):
        print("close.. main window")
        self._signal._mainClose.emit(1)
        super().closeEvent(e)

# 关联窗口 - 主窗口
class RMainWidget(QWidget):
    def __init__(self):
        super().__init__()
        self._signal = Signal()

    def closeEvent(self, e):
        print("close.. main widget")
        self._signal._mainClose.emit(1)
        super().closeEvent(e)


# 关联窗口 widget - 子窗口
class RSubWidget(QWidget):
    def __init__(self):
        super().__init__()
        _signal = Signal()
        _signal._mainClose[int].connect(self._sub_close)

    def _sub_close(self, flag):
        if flag:
            print("sub close")
            self.close()

# 关联窗口 mainwindow - 子窗口
class RSubMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        _signal = Signal()
        _signal._mainClose[int].connect(self._sub_close)

    def _sub_close(self, flag):
        if flag:
            print("sub close")
            self.close()


文章评论

0条评论