在QMainWindow的MVC模式中,我们可以通过重写closeEvent方法来处理窗口关闭事件。closeEvent方法是QMainWindow的一个事件处理函数,当用户关闭窗口时会自动调用该方法。
在closeEvent方法中,我们可以通过以下步骤从非QObject调用方法:
下面是一个示例代码:
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import QObject, pyqtSignal
import sys
class CustomClass(QObject):
def __init__(self):
super().__init__()
def customMethod(self):
print("Custom method called")
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.customObj = CustomClass()
def closeEvent(self, event):
self.customObj.customMethod()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
在上述代码中,我们创建了一个CustomClass类,其中定义了一个customMethod方法用于演示从非QObject调用方法。在MainWindow的构造函数中,我们创建了CustomClass的实例并将其保存在self.customObj成员变量中。在closeEvent方法中,我们通过self.customObj调用customMethod方法。
这样,在QMainWindow的closeEvent方法中,我们就可以从非QObject调用方法了。
关于PyQt的更多信息和使用方法,可以参考腾讯云的PyQt产品文档:PyQt产品介绍
没有搜到相关的结果
领取专属 10元无门槛券
手把手带您无忧上云