PYQT5:如何与自定义消息框一起使用CloseEvent

发布于 2025-02-05 07:25:14 字数 1593 浏览 1 评论 0原文

在关闭主窗口之前,我想做一些最后的操作。 我认为collectevent是正确的选择,但是标准的Qmessagebox不适合我的设计,因此我想自己制作(并且我做到了)。但是,使用以下代码,该应用程序直接关闭而无需显示我的消息框。

这是什么问题?

def closeEvent(self, event):
    self.messageBox = lastWindow(self)
    self.messageBox.show()
    return super().closeEvent(event)

这是lastWindow代码:(生成.UI文件的pyuic5的子类):

from PyQt5 import QtCore
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qt, QPoint 
from PyQt5.QtWidgets import QDialog, QGraphicsDropShadowEffect

from UIs.UI_quit import Ui_Quit

class lastWindow(QDialog, Ui_Quit):
    def __init__(self, parent = None):
        super(lastWindow, self).__init__(parent)
        self.setupUi(self)
        self._parent = parent
        self.setWindowFlag(QtCore.Qt.WindowType.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
        self.setAttribute(QtCore.Qt.WidgetAttribute.WA_DeleteOnClose)
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(10)
        self.shadow.setXOffset(5)
        self.shadow.setYOffset(5)
        self.shadow.setColor(QColor(0, 0, 0, 80))
        self.mainFrame.setGraphicsEffect(self.shadow)
        self.btn_close.clicked.connect(self.abort)
        self.btn_quit.clicked.connect(self.destroy)
        self.btn_abort.clicked.connect(self.abort)
        self._parent.blur.setEnabled(True)

    def destroy(self):
        self._parent.close()
        self.close()

    def abort(self):
        self._parent.startService()
        self.close()

Before I close my main window, I want to do some last operations.
I think the closeEvent is the right thing to do that, but the standard QMessageBox does not suit my design, so I want to make my own (and I did). But with the following code, the application closes directly without showing my message-box.

What here is the problem?

def closeEvent(self, event):
    self.messageBox = lastWindow(self)
    self.messageBox.show()
    return super().closeEvent(event)

This is the lastWindow code: (subclass of a pyuic5 generated .ui file):

from PyQt5 import QtCore
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qt, QPoint 
from PyQt5.QtWidgets import QDialog, QGraphicsDropShadowEffect

from UIs.UI_quit import Ui_Quit

class lastWindow(QDialog, Ui_Quit):
    def __init__(self, parent = None):
        super(lastWindow, self).__init__(parent)
        self.setupUi(self)
        self._parent = parent
        self.setWindowFlag(QtCore.Qt.WindowType.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
        self.setAttribute(QtCore.Qt.WidgetAttribute.WA_DeleteOnClose)
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(10)
        self.shadow.setXOffset(5)
        self.shadow.setYOffset(5)
        self.shadow.setColor(QColor(0, 0, 0, 80))
        self.mainFrame.setGraphicsEffect(self.shadow)
        self.btn_close.clicked.connect(self.abort)
        self.btn_quit.clicked.connect(self.destroy)
        self.btn_abort.clicked.connect(self.abort)
        self._parent.blur.setEnabled(True)

    def destroy(self):
        self._parent.close()
        self.close()

    def abort(self):
        self._parent.startService()
        self.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2025-02-12 07:25:14

使用内置接受 refffect lots 插槽以关闭对话框,然后使用 exec

class lastWindow(QDialog, Ui_Quit):
    ...
    def destroy(self):
        self.accept()

    def abort(self):
        self.reject()
    
class MainWindow(QMainWindow):
    ...
    def closeEvent(self, event):
        dialog = lastWindow(self)
        if dialog.exec() == QDialog.Rejected:
            self.startService()
            event.ignore()

> event.ignore()将阻止窗口关闭;否则,默认情况下将接受该事件,并且窗口将关闭。请注意,与show()不同,exec()呼叫块(即等待用户互动),这解释了为什么您的当前示例无法正常工作, 。

Use the built-in accept and reject slots to close the dialog, and then use the return value of exec in your closeEvent to decide what to do next:

class lastWindow(QDialog, Ui_Quit):
    ...
    def destroy(self):
        self.accept()

    def abort(self):
        self.reject()
    
class MainWindow(QMainWindow):
    ...
    def closeEvent(self, event):
        dialog = lastWindow(self)
        if dialog.exec() == QDialog.Rejected:
            self.startService()
            event.ignore()

Calling event.ignore() will prevent the window closing; otherwise, the event will be accepted by default and the window will close. Note that, unlike show(), the exec() call blocks (i.e. it waits for user interaction), which explains why your current example doesn't work as you expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文