PYQT5:如何与自定义消息框一起使用CloseEvent
在关闭主窗口之前,我想做一些最后的操作。 我认为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用内置接受 和 refffect lots 插槽以关闭对话框,然后使用 exec
> 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:Calling
event.ignore()
will prevent the window closing; otherwise, the event will be accepted by default and the window will close. Note that, unlikeshow()
, theexec()
call blocks (i.e. it waits for user interaction), which explains why your current example doesn't work as you expected.