简体   繁体   English

PySide关闭窗口函数接收自定义参数

[英]PySide close window function receiving custom arguments

I have main function from within I'm creating one instance of the Control class and one instance of the GuiWindow class. 我在创建Control类的一个实例和GuiWindow类的一个实例中具有主要功能。 This GuiWindow object have methods that accepts the previous mentioned instance of the Control class as their arguments. 这个GuiWindow对象具有接受前面提到的Control类实例作为其参数的方法。 But GuiWindow doesn't have Control object as its own attribute! 但是GuiWindow没有将Control对象作为自己的属性! It receives it as his own method's arguments when called from main function. 从main函数调用时,它将其作为自己方法的参数接收。 So everything works, GuiWindow has event listeners and in accordance with them he calls Control and Control does whatever it needs to do... But Control needs to do something very important when user closes GUI window! 因此一切正常,GuiWindow具有事件侦听器,并且根据它们,他将其称为Control,并且Control会执行其需要做的一切……但是,当用户关闭GUI窗口时,Control需要做非常重要的事情!

All my current closeEvent function does is the following: 我当前所有的closeEvent函数都执行以下操作:

def closeEvent(self, event):

    quit_msg = "Are you sure you want to exit the program?"
    reply = QtGui.QMessageBox.question(self, 'Exit',
            quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

    if reply == QtGui.QMessageBox.Yes:
        event.accept()
        exit()
    else:
        event.ignore()

What if I want to pass object of Control class to this closeEvent function? 如果我想将Control类的对象传递给此closeEvent函数怎么办? Is that possible? 那可能吗? I don't know how to overwrite the closeEvent function so that it knows that it needs to call some Control method. 我不知道如何覆盖closeEvent函数,以便它知道它需要调用某些Control方法。 If I put Control class object as the closeEvent function arguments it doesn't work of course... 如果我将Control类对象作为closeEvent函数参数,那当然是行不通的...

Has anyone any idea? 有人知道吗? Should I use some kind of Signal and Event mechanism? 我应该使用某种信号和事件机制吗?

I managed to get my closeEvent function call method of my Control class by using Signal and Event mechanism. 我设法通过使用信号和事件机制来获取Control类的closeEvent函数调用方法。 I created the following class: 我创建了以下类:

class ExitApp(QtCore.QObject):
    exit_app_signal = QtCore.Signal(str)

    def __init__(self):
        QtCore.QObject.__init__(self)

    def exit_app(self, exit_message):
        self.exit_app_signal.emit(exit_message)

which object I connected to the desired method of my Control class, and which I called in the following manner: 我将哪个对象连接到Control类的所需方法,并以以下方式调用了该对象:

def closeEvent(self, event):

    quit_msg = "Are you sure you want to exit the program?"
    reply = QtGui.QMessageBox.question(self, 'Exit',
            quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

    if reply == QtGui.QMessageBox.Yes:
        self.exit_app.exit_app('Properly exit.')
        event.accept()
        exit()
    else:
        event.ignore() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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