qt - 在 QT 中关闭窗口之前获取应用程序退出信号

标签 qt pyqt pyqt5

我有一个带有多个主窗口的 Qt 应用程序,我想在应用程序即将退出时收到一个信号,而所有窗口仍处于打开状态。

QApplication.aboutToQuit 信号对我不起作用,因为它只在所有窗口关闭后触发。

我看到的所有其他答案都建议实现主窗口的 closeEvent() 函数,但我有多个主窗口,我找不到任何方法来区分 CloseEvent 在仅关闭单个窗口的正常 closeEventQMD+Q 之后整个应用程序关闭或在任务中使用单击退出时的关闭事件之间bar 或出于任何原因退出。

如何仅在整个应用程序即将退出但在所有窗口关闭之前获得信号?

当我按下 Cmd+Q 或右键单击退出任务栏图标时会发生什么:

  1. (此时我想要一个信号)<-
  2. 所有窗口都有一个 closeEvent()
  3. aboutToQuit() 触发
  4. 应用退出

我想要的是在发生任何事情之前得到一个信号,并且所有的 window 仍然打开。

编辑:最小示例

from PyQt5.QtGui import QCloseEvent
from PyQt5.QtWidgets import QApplication, QWidget

class Win(QWidget):
    def closeEvent(self, event: QCloseEvent):
        # implementing a custom closeEvent doesn't help me
        # this is called for every normal manual window close and when the application quits
        # I only want to run something when the full application gets shut down, not just this window
        # can't see any way to differentiate between the two
        print("closeEvent", event.type(), event)
        return super().closeEvent(event)


if __name__ == '__main__':
    app = QApplication([])
    app.aboutToQuit.connect(lambda :print("about to quit, this is too late, by now all windows are closed"))

    #What I need
    # app.beforeQuitSignal.connect(lambda :print("signal that it's gonna quit, before any of the windows are closed"))

    #stuff I've tried that didn't work either
    app.quit = lambda *args:print("quit, doesnt get called")
    app.exit = lambda *args:print("exit, doesnt get called")
    app.closeAllWindows = lambda *args:print("closeAllWindows, doesnt get called")

    mainwins = []
    for i in range(5):
        win = Win()
        win.setGeometry(100*i,100*i, 400,400), win.show(), win.raise_()
        mainwins.append(win)

    app.exec_()

最佳答案

如果你想在用户关闭窗口时触发一个信号,但不在最后一个窗口关闭后立即退出 QApplication,你可以使用 QApplication.setQuitOnLastWindowClosed()

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtWidgets import QApplication, QWidget


class Win(QWidget):
    closing = pyqtSignal()

    def closeEvent(self, event: QCloseEvent):
        print("Window {} closed".format(self))
        self.closing.emit()
        return super().closeEvent(event)


class MyApp(QApplication):
    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

        self.setQuitOnLastWindowClosed(False)
        self.lastWindowClosed.connect(self.onLastClosed)

        self.mainwins = []
        for i in range(5):
            win = Win()
            win.setGeometry(100 * i, 100 * i, 400, 400), win.show(), win.raise_()
            self.mainwins.append(win)

    def onLastClosed(self):
        print("Last windows closed, exiting ...")
        self.exit()


if __name__ == '__main__':
    app = MyApp([])
    app.exec_()

有了这个,当一个窗口关闭时,一个 closing() 信号被发出

关于qt - 在 QT 中关闭窗口之前获取应用程序退出信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47761219/

相关文章:

c++ - 在设计时向 QStackedWidget 添加页面 Qt Designer

c++ - 为什么在 Qt C++ 中关闭主窗口后打印消息?

python - 如何在 QComboBox 中居中文本?

Python-PyQt : restarting a thread which is finished

c++ - Code::Blocks 预构建选项与命令行 (Qt) 不一致

c++ - Qt、c++ QML 和 HWND

javascript - 插件如何增强 Anki 的 JavaScript?

python - 绘制 PyQt 小部件背景时出现意外消息

python - pyqt在滚动区域展开网格

python - 如何在 QHorizo​​ntalBarSeries/QChart 的指定 x 值处画一条垂直线?