首页 文章

QDialog - 防止在Python和PyQt中关闭

提问于
浏览
2

我有一个使用pyqt和python编写的登录屏幕对话框,它在运行时会显示一个对话框,你可以输入一个certin用户名和密码来解锁它 . 这只是我在学习pyqt时所做的简单 . 我正试图在其他地方使用它,但需要知道是否有办法阻止某人使用x按钮并关闭它我想让它保持在所有窗口的顶部,所以它不能被移出的方式?这可能吗?我做了一些研究,找不到任何可以帮助我的东西 .

Edit:

这里要求的是代码:

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

2 回答

  • 9

    首先是坏消息,根据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)
    
  • 2

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

相关问题