[英]QDialog - Prevent Closing in Python and PyQt
I have a login screen dialog written using pyqt and python and it shows a dialog pup up when it runs and you can type in a certin username and password to unlock it basicly.我有一个使用 pyqt 和 python 编写的登录屏幕对话框,它在运行时会显示一个对话框小狗,您可以输入一个特定的用户名和密码来基本解锁它。 It's just something simple I made in learning pyqt.
这只是我在学习 pyqt 时做的一些简单的事情。 I'm trying to take and use it somewhere else but need to know if there is a way to prevent someone from using the x button and closing it i would like to also have it stay on top of all windows so it cant be moved out of the way?
我正在尝试在其他地方使用它,但需要知道是否有办法阻止某人使用 x 按钮并关闭它的方式? Is this possible?
这可能吗? I did some research and couldn't find anything that could help me.
我做了一些研究,找不到任何可以帮助我的东西。
Edit:编辑:
as requested here is the code:这里要求的是代码:
from PyQt4 import QtGui
class Test(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.textUsername = QtGui.QLineEdit(self)
self.textPassword = QtGui.QLineEdit(self)
self.loginbuton = QtGui.QPushButton('Test Login', self)
self.loginbuton.clicked.connect(self.Login)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.textUsername)
layout.addWidget(self.textPassword)
layout.addWidget(self.loginbuton)
def Login(self):
if (self.textUsername.text() == 'Test' and
self.textPassword.text() == 'Password'):
self.accept()
else:
QtGui.QMessageBox.warning(
self, 'Wrong', 'Incorrect user or password')
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
if Test().exec_() == QtGui.QDialog.Accepted:
window = Window()
window.show()
sys.exit(app.exec_())
Bad news first, it is not possible to remove the close button from the window, based on the Riverbank mailing system首先是坏消息,无法从窗口中删除关闭按钮,基于Riverbank 邮件系统
You can't remove/disable close button because its handled by the window manager, Qt can't do anything there.
您不能删除/禁用关闭按钮,因为它是由窗口管理器处理的,Qt 在那里不能做任何事情。
Good news, you can override and ignore, so that when the user sends the event, you can ignore or put a message or something.好消息,你可以覆盖和忽略,这样当用户发送事件时,你可以忽略或放一条消息什么的。
Read this article for ignoring the QCloseEvent
阅读这篇文章忽略
QCloseEvent
Also, take a look at this question, How do I catch a pyqt closeEvent and minimize the dialog instead of exiting?另外,看看这个问题, 如何捕获 pyqt closeEvent 并最小化对话框而不是退出?
Which uses this:哪个使用这个:
class MyDialog(QtGui.QDialog):
# ...
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
# when you want to destroy the dialog set this to True
self._want_to_close = False
def closeEvent(self, evnt):
if self._want_to_close:
super(MyDialog, self).closeEvent(evnt)
else:
evnt.ignore()
self.setWindowState(QtCore.Qt.WindowMinimized)
I don't know if you want to do this but you can also make your window frameless.我不知道你是否想这样做,但你也可以让你的窗户无框。 To make window frameless you can set the window flag equal to QtCore.Qt.FramelessWindowHint
要使窗口无框架,您可以将窗口标志设置为QtCore.Qt.FramelessWindowHint
You can disable the window buttons in PyQt5.您可以禁用 PyQt5 中的窗口按钮。
The key is to combine it with "CustomizeWindowHint", and exclude the ones you want to be disabled.关键是将它与“CustomizeWindowHint”结合起来,并排除您要禁用的那些。
Example:例子:
#exclude "QtCore.Qt.WindowCloseButtonHint" or any other window button
self.setWindowFlags(
QtCore.Qt.Window |
QtCore.Qt.CustomizeWindowHint |
QtCore.Qt.WindowTitleHint |
QtCore.Qt.WindowMinimizeButtonHint
)
Result with QDialog
:结果与
QDialog
:
Reference: https://doc.qt.io/qt-5/qt.html#WindowType-enum参考: https : //doc.qt.io/qt-5/qt.html#WindowType-enum
Tip: if you want to change flags of the current window, use window.show()
after window.setWindowFlags
, because it needs to refresh it, so it calls window.hide()
.提示:如果你想改变当前窗口,使用的标志
window.show()
后window.setWindowFlags
,因为它需要刷新它,所以它调用window.hide()
Tested with QtWidgets.QDialog
on: Windows 10 x32, Python 3.7.9, PyQt5 5.15.1 .使用
QtWidgets.QDialog
测试: Windows 10 x32、Python 3.7.9、PyQt5 5.15.1。
Have a great day everyone!祝大家有个美好的一天!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.