首页 > 解决方案 > PySide2:如何防止 QFileDialog 关闭 Sub-QMainWidget

问题描述

我有一个继承自 QMainWindow 的超类,它的目的是调用 Connection 函数中的一个函数,而不是显示启动器并杀死调用者的普通 CloseEvent (这将关闭我的整个应用程序),并且只有当它发送关闭时,实际关闭应用程序。

无论如何,我现在正试图在函数内的一个派生类中调用 QFileDialog,只要它关闭(接受、拒绝),就会触发 CloseEvent。这里的问题是我的类被识别为调用者,我看不出有任何方法可以将 Close 事件与对话框和 MainWindow 区分开来。该事件没有方法或属性“发送者”,并且 self 当然始终保留在 QMainWindow 类(或其派生的超类)中......

我的超类:

#import 3rd-Party-Libs
from PySide2.QtWidgets import QMainWindow

class SuperMiniApp(QMainWindow):
    '''SuperApp for the common MiniApp-Functions'''
    def __init__(self, parent):
        '''initialize this class'''
        super(SuperMiniApp, self).__init__(parent)

    def closeEvent(self, event):
        '''Overwrite QMainWindow's closeEvent, to prevent it from closing the whole App'''
        #if not from starter catch the Close-Event
        if (self == self.parent()._starter):
            event.accept()
        else:
            self.parent().close_MiniApp(self, event)

调用 QFileDialog:

def call_dialog(self):
    title = 'Choose One'
    datatypes = '*.*'
    dir = os.getcwd()
    return(QFileDialog.getOpenFileName(self, title, dir, datatypes)[0])

标签: pythonpyside2

解决方案


推荐阅读