生成 QCloseEvent 不会关闭 QMainWindow [英] Generating a QCloseEvent won't close QMainWindow

查看:53
本文介绍了生成 QCloseEvent 不会关闭 QMainWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些非常简单的事情:添加一个带有 Exit 操作的菜单栏,该操作将在选择时关闭 QMainWindow.但是,当我实际单击 Exit 时,它不会关闭应用程序.SSCCE:

I'm trying to do something quite simple: add a menu bar with an Exit action that will close a QMainWindow when it is selected. However, when I actually click Exit, it doesn't close the application. A SSCCE:

from PyQt4 import QtGui, QtCore

import sys

class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        widget = QtGui.QWidget()
        self.setCentralWidget(widget)

        self.menu_bar = QtGui.QMenuBar(self)
        menu = self.menu_bar.addMenu('File')

        exit_action = QtGui.QAction('Exit', self)
        exit_action.triggered.connect(lambda:
            self.closeEvent(QtGui.QCloseEvent()))
        menu.addAction(exit_action)
        self.setMenuBar(self.menu_bar)

    def closeEvent(self, event):
        print('Calling')
        print('event: {0}'.format(event))
        event.accept()


app = QtGui.QApplication(sys.argv)
form = Window()
form.show()
sys.exit(app.exec_())

真正让我困惑的是,当我从 File 菜单中单击 Exit 时,我得到以下输出:

What is really confusing me is that when I click Exit from the File menu, I get the following output:

打电话

事件:

event: <PyQt4.QtGui.QCloseEvent object at 0x024B7348>

并且应用程序没有退出.

and the application does not exit.

如果我点击右上角的 X,我会得到同样的结果(直到事件对象的相同内存地址):

If I click the top-right corner X, I get the same thing (down to the same memory address for the event object):

打电话

事件:

event: <PyQt4.QtGui.QCloseEvent object at 0x024B7348>

应用程序确实退出.

这是在 Windows 7 64 位、Python 2.7.2、PyQt 4.8.6 上.

This is on Windows 7 64-bit, Python 2.7.2, PyQt 4.8.6.

推荐答案

Document says

QCloseEvent 类包含描述关闭事件的参数.

The QCloseEvent class contains parameters that describe a close event.

关闭事件发送到用户想要关闭的小部件,通常通过从窗口菜单中选择关闭",或单击 X 标题栏按钮.当您调用 QWidget.close() 关闭时,它们也会被发送以编程方式创建一个小部件.

Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget.close() to close a widget programmatically.

你可以不通过QCloseEvent直接用信号关闭调用,请调用self.close().

Your can call directly with signal close not by QCloseEvent, please call self.close().

from PyQt4 import QtGui, QtCore

import sys

class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        widget = QtGui.QWidget()
        self.setCentralWidget(widget)

        self.menu_bar = QtGui.QMenuBar(self)
        menu = self.menu_bar.addMenu('File')

        exit_action = QtGui.QAction('Exit', self)
        exit_action.triggered.connect(self.close)
        menu.addAction(exit_action)
        self.setMenuBar(self.menu_bar)

    def closeEvent(self, event):
        print('Calling')
        print('event: {0}'.format(event))
        event.accept()


app = QtGui.QApplication(sys.argv)
form = Window()
form.show()
sys.exit(app.exec_())

这篇关于生成 QCloseEvent 不会关闭 QMainWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆