处理主窗口PyQt中的关闭按钮的正确方法,(红色“X”)

时间:2014-07-02 13:11:49

标签: python qt qt4 pyqt pyqt4

首先,我是PyQt的新手。

我一直试图将一个功能链接到主窗口的关闭按钮(窗口一角的红色x),但我没有取得任何成功。现在,我的代码看起来像这样:

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
    def setupUi(self, MainWindow):
        #setup code goes here
    def retranslateUi(self, MainWindow):
        #re translation of the GUI code 
    def closeEvent(self, event):
        print "User has clicked the red x on the main window"

在一个单独的“主”文件中,我有以下内容:

class GUIForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        #self.ui.ECUStatus_txt = MyWidget.__init__.text_list
        self.threadData()


    if __name__ == "__main__":

        app = QtGui.QApplication(sys.argv)
        myapp = GUIForm()
        myapp.show()
        ret = app.exec_()
        sys.exit(ret)

但是,当我通过命令提示符运行时,当我点击红色x时,我无法看到print语句。我知道我是Qt的新手,但是我看到很多人都在问这个问题,而且没有一个答案似乎超出了上面已经写过的内容。

其中一个答案: Answer #1 Answer #2

这两种解决方案都与我的相似,但它仍然不起作用

尽管答案可能对该用户的特定代码起作用,但我的同事PyQt同事仍然对我们不起作用的推理充满了迷雾。是否为PyQt内置的“Red X box”定义了按钮名称?我可以将它连接到另一个功能吗?我会用于其他按钮吗?

3 个答案:

答案 0 :(得分:7)

您不应该修改从您的ui文件生成的类。相反,你应该子类化并修改子类。

从代码的外观来看,实际上是在创建两个QMainWindow并且正在捕获错误的closeEvent(可能是隐藏了一个?)。即self.ui是未显示的QMainWindow,并且未添加到GUIForm的用户界面中。相反,您自己明确使用Ui_MainWindow.setupUi()方法,将小部件添加到您自己的QMainWindow,'GUIForm`。

相反,您应该做的是将Ui_MainWindow类保留为从ui文件生成时的类,然后将主python文件修改为:

class GUIForm(Ui_MainWindow):
    def __init__(self, parent=None):
        Ui_MainWindow.__init__(self, parent)
        self.threadData()

    def closeEvent(self, event):
        print "User has clicked the red x on the main window"
        event.accept()


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    myapp = GUIForm()
    myapp.show()
    ret = app.exec_()
    sys.exit(ret)

这样您就可以扩展自动生成的UI文件的行为。这样可以轻松地从.ui文件重新生成python文件,而无需重新添加代码(这正是您永远不应该修改自动生成的Python文件的原因)

答案 1 :(得分:3)

有时,在处理来自主窗口的事件信号时会出现问题。

您可以使用以下代码:

app.aboutToQuit.connect(self.closeEvent)

您可以在 closeEvent 函数中编写自己的代码。

注意:

app QtGui.QApplication 实例的名称

以下是完整代码的演示:

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        #{================================

        app.aboutToQuit.connect(self.closeEvent)

        #}================================

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle('Demo')

    #{================================

    def closeEvent(self):
        #Your desired functionality here
        print('Close button pressed')
        import sys
        sys.exit(0)

    #}================================


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

希望它有所帮助。

答案 2 :(得分:1)

我遇到了同样的问题,在该对话框中有一个对话框提示,要求用户在进行操作之前进行输入。用户可以在处理操作之前添加输入,也可以跳过添加输入和处理,或者使用“取消”按钮或单击对话框窗口的X取消操作。

我要做的是创建一个具有布尔值的变量,该变量将在关闭窗口之前进行检查,以便在强制关闭窗口的情况下,很明显进程已中止。

class Ui_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)
        #This variable will be your way of determining how the window was closed
        self.force_close = True
        self.buttonOk.clicked.connect(self.someOtherFunction)

    def setupUi(self, MainWindow):
        #setup code goes here

    def retranslateUi(self, MainWindow):
        #re translation of the GUI code 

    def someOtherFunction(self):
        # Do some things here if needed
        print('All done')
        # Assuming some operation is performed and a value or result is generated            
        # Since this function completed, there is no need to force close the window
        # So it can be set to False
        self.force_close = False
        # the below close() is a built-in function and will automatically trigger the
        # closeEvent
        self.close()

    def closeEvent(self, event):            
        if self.force_close is True:
            # Here is where you could add you code to perform some operation
            # due to the user clicking the X