Python Pyqt5 QMDIarea子类,具有关闭时的自定义信号

Python Pyqt5 QMDIarea子类,具有关闭时的自定义信号,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我需要在qmdusibwindowclose上创建一个自定义信号。换句话说,当我关闭任何子窗口时,会发出一个信号,该窗口的名称将被关闭。下面是我的线索,但似乎不对。错误发生的原因如下: 已创建子窗口,但未调用 “添加子窗口”选项不起作用 可关闭操作不起作用 希望你能告诉我怎么修 import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class MyMdi(QM

我需要在
qmdusibwindow
close上创建一个自定义信号。换句话说,当我关闭任何子窗口时,会发出一个信号,该窗口的名称将被关闭。下面是我的线索,但似乎不对。错误发生的原因如下:

  • 已创建子窗口,但未调用
  • “添加子窗口”选项不起作用
  • 可关闭操作不起作用
  • 希望你能告诉我怎么修

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    
    class MyMdi(QMdiSubWindow):
    
       sigClosed = pyqtSignal(str)
    
       def __init__(self, parent=None):
          super(MyMdi, self).__init__(parent)
          
    
       def closeEvent(self, event):
          """Get the name of active window about to close
          """
          name = name
          self.sigClosed.emit('{} is close'.format(name))
          QMdiSubWindow.closeEvent(self, event)
    
    
    
    class MainWindow(QMainWindow):
       count = 0
        
       def __init__(self, parent = None):
          super(MainWindow, self).__init__(parent)
          
          self.mdi = MyMdi()
          self.setCentralWidget(self.mdi)
          bar = self.menuBar()
            
          file = bar.addMenu("File")
          file.addAction("New")
          file.triggered[QAction].connect(self.windowaction)
          self.setWindowTitle("MDI demo")
    
          # my signal
          self.mdi.sigClosed.connect(self.windowclosed)
    
       @pyqtSlot(str)
       def windowclosed(self, text):
          print(text)
    
            
       def windowaction(self, q):
            
          if q.text() == "New":
             MainWindow.count = MainWindow.count+1
             sub = QMdiSubWindow()
             sub.setWidget(QTextEdit())
             sub.setWindowTitle("subwindow"+str(MainWindow.count))
             self.mdi.addSubWindow(sub)
             sub.show()
            
    def main():
       app = QApplication(sys.argv)
       ex = MainWindow()
       ex.show()
       sys.exit(app.exec_())
        
    if __name__ == '__main__':
       main()
    

    您有一个初始错误:QMdiSubWindow必须位于QMdiArea内,但代码中没有

    另一方面,子类化的想法很好,但有几个缺点:

    • 由于没有QMdiArea,您最初没有使用它,如果您执行QAction,那么您的应用程序将被关闭,因为QMdiSubWindow没有任何称为addSubWindow的方法

    • QMB窗口没有名为name的属性,必须使用windowTitle

    类MdiSubWindow(QMBwindow):
    sigClosed=pyqtSignal(str)
    def关闭事件(自身、事件):
    “”“获取要关闭的活动窗口的名称。”
    """
    self.sigClosed.emit(self.windowTitle())
    QMBwindow.closeEvent(自我,事件)
    类主窗口(QMainWindow):
    计数=0
    def uuu init uuu(self,parent=None):
    超级(主窗口,自我)。\uuuuu初始化\uuuuuuu(父级)
    self.mdi=QMdiArea()
    self.setCentralWidget(self.mdi)
    bar=self.menuBar()
    file=bar.addMenu(“文件”)
    file.addAction(“新建”)
    文件。已触发[QAction]。连接(self.windowaction)
    self.setWindowTitle(“MDI演示”)
    @pyqtSlot(str)
    def窗口已关闭(自身,文本):
    打印(文本)
    def窗口操作(自身,q):
    如果q.text()=“新建”:
    MainWindow.count=MainWindow.count+1
    sub=MdiSubWindow()
    sub.setWidget(QTextEdit())
    sub.setAttribute(Qt.WA_DeleteOnClose)
    sub.setWindowTitle(“subwindow”+str(MainWindow.count))
    sub.sigClosed.connect(自关闭窗口)
    self.mdi.addSubWindow(子窗口)
    
    sub.show()
    工作正常,谢谢。一个查询,当您调用“sub.close()”时;这会删除子窗口及其小部件吗?或者我必须调用deleteLater()?默认情况下,否,因此我添加了
    sub.setAttribute(Qt.WA_DeleteOnClose)
    ,它在窗口关闭时调用deleteLater()。
    class MdiSubWindow(QMdiSubWindow):
        sigClosed = pyqtSignal(str)
    
        def closeEvent(self, event):
            """Get the name of active window about to close
          """
            self.sigClosed.emit(self.windowTitle())
            QMdiSubWindow.closeEvent(self, event)
    
    
    class MainWindow(QMainWindow):
        count = 0
    
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            self.mdi = QMdiArea()
            self.setCentralWidget(self.mdi)
            bar = self.menuBar()
    
            file = bar.addMenu("File")
            file.addAction("New")
            file.triggered[QAction].connect(self.windowaction)
            self.setWindowTitle("MDI demo")
    
        @pyqtSlot(str)
        def windowclosed(self, text):
            print(text)
    
        def windowaction(self, q):
            if q.text() == "New":
                MainWindow.count = MainWindow.count + 1
                sub = MdiSubWindow()
                sub.setWidget(QTextEdit())
                sub.setAttribute(Qt.WA_DeleteOnClose)
                sub.setWindowTitle("subwindow" + str(MainWindow.count))
                sub.sigClosed.connect(self.windowclosed)
                self.mdi.addSubWindow(sub)
                sub.show()