【问题标题】:QDialog - Prevent Closing in Python and PyQtQDialog - 防止在 Python 和 PyQt 中关闭
【发布时间】:2013-08-15 15:49:17
【问题描述】:

我有一个使用 pyqt 和 python 编写的登录屏幕对话框,它在运行时会显示一个对话框小狗,您可以输入一个特定的用户名和密码来基本解锁它。这只是我在学习pyqt时做的一些简单的事情。我正在尝试在其他地方使用它,但需要知道是否有办法阻止某人使用 x 按钮并关闭它的方式?这可能吗?我做了一些研究,找不到任何可以帮助我的东西。

编辑:

这里要求的是代码:

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_())

【问题讨论】:

  • 你能贴一些代码吗?
  • 让我一秒钟把它从我的 pi 上取下来

标签: python pyqt


【解决方案1】:

首先是坏消息,不可能从窗口中移除关闭按钮,基于Riverbank mailing system

您无法移除/禁用关闭按钮,因为它由 窗口管理器,Qt 不能在那里做任何事情。

好消息,你可以覆盖和忽略,这样当用户发送事件时,你可以忽略或者放一个消息什么的。

Read this article for ignoring the QCloseEvent

另外,看看这个问题,How do I catch a pyqt closeEvent and minimize the dialog instead of exiting?

哪个使用这个:

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)

【讨论】:

  • 好的,谢谢,但我还有一个问题,如果您可以更新您的代码以防止用户点击开箱即用,这样它就会显示在顶部而您无法点击它?
  • @raspberry.pi 我没有更多时间编写代码,但是您可以通过查看pyqt windows flags来实现您想要的目标
  • 现在我遇到了一个问题,一个按钮覆盖了另一个按钮,我认为我正在使用 layout.add.Widget(self.closebutton)
  • 我的意思是编程
【解决方案2】:

您可以在 PyQt5 中禁用窗口按钮。

关键是将它与“CustomizeWindowHint”结合起来, 并排除您要禁用的那些。

示例:

#exclude "QtCore.Qt.WindowCloseButtonHint" or any other window button

self.setWindowFlags(
    QtCore.Qt.Window |
    QtCore.Qt.CustomizeWindowHint |
    QtCore.Qt.WindowTitleHint |
    QtCore.Qt.WindowMinimizeButtonHint
    )

QDialog 的结果:

参考:https://doc.qt.io/qt-5/qt.html#WindowType-enum

提示:如果要更改当前窗口的标志,请使用window.show()window.setWindowFlags 之后, 因为需要刷新,所以调用window.hide()

QtWidgets.QDialog 上测试: 视窗 10 x32, 蟒蛇3.7.9, PyQt5 5.15.1 .

祝大家有美好的一天!

【讨论】:

  • 关于“排除你想禁用的那些”的部分对我来说是关键。
【解决方案3】:

我不知道你是否想这样做,但你也可以让你的窗框无框。要使窗口无框,您可以将窗口标志设置为 QtCore.Qt.FramelessWindowHint

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多