PyQt:单击 X 不会触发 closeEvent [英] PyQt: clicking X doesn't trigger closeEvent

查看:146
本文介绍了PyQt:单击 X 不会触发 closeEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PyQt 的新手,试图开发简单的应用程序.我用 Qt-designer 设计了简单的 ui.如果用户真的想在单击 X 或 ,,Exit'' 按钮或从菜单中选择退出时退出应用程序,我需要额外确认.

I'm a total newbie in PyQt trying to develop simple application. I have designed simple ui with Qt-designer. I want extra confirmation if the user really want to exit application when clicking X or ,,Exit'' button or choosing Exit from menu.

代码如下:

import sys
from PyQt4 import QtGui, QtCore, uic

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.ui = uic.loadUi('main_window.ui')
        self.ui.show()

        self.ui.btnExit.clicked.connect(self.close)
        self.ui.actionExit.triggered.connect(self.close)

    def closeEvent(self, event):
        print("event")
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

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


def main():
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

问题在于:

  • 当我在主窗口点击 X 时,closeEvent 函数不会触发
  • 当我单击退出"按钮或从菜单中选择,,退出"时,功能
    被调用,但单击是不会关闭应用程序.

我在 SO 上发现了一些问题并搜索了教程,但没有涵盖此类问题.我做错了什么?

I have found some questions on SO and searched for tutorials, but nothing covered such problem. What am I doing wrong?

推荐答案

注意你在做什么:

self.ui = uic.loadUi('main_window.ui')
self.ui.show()

你的实际窗口是一个实例属性(ui)inside win.不是 win 本身.它没有实现 closeEvent.

Your actual window is an instance attribute (ui) inside win. Not the win itself. And it doesn't have closeEvent implemented.

loadUi 可以加载.ui 文件在一个实例中.

PyQt4.uic.loadUi(uifile[, baseinstance=None[, package='']])

你应该使用它.这样,您的代码将是:

You should use that. With that, your code would be:

import sys
from PyQt4 import QtGui, QtCore, uic

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        uic.loadUi('main_window.ui', self)

        self.btnExit.clicked.connect(self.close)
        self.actionExit.triggered.connect(self.close)

    def closeEvent(self, event):
        print("event")
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

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


def main():
    app = QtGui.QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

注意:我不喜欢在 __init__show 显示窗口.明确的更好.所以,我把它移到了 main.随意修改它.

Note: I'm not a fan of showing the window in __init__. Explicit is better. So, I moved that to main. Feel free to modify it.

这篇关于PyQt:单击 X 不会触发 closeEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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