QDialog – Prevent Closing in Python and PyQt

Question:

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. It’s just something simple I made in learning 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? 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_())
Asked By: raspberry.pi

||

Answers:

Bad news first, it is not possible to remove the close button from the window, based on the Riverbank mailing system

You can’t remove/disable close button because its handled by the
window manager, Qt can’t do anything there.

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

Also, take a look at this question, How do I catch a pyqt closeEvent and minimize the dialog instead of exiting?

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)
Answered By: user1786283

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

Answered By: Eli

You can disable the window buttons in PyQt5.

The key is to combine it with "CustomizeWindowHint",
and exclude the ones you want to be disabled.

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:
enter image description here

Reference: 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().

Tested with QtWidgets.QDialog on:
Windows 10 x32,
Python 3.7.9,
PyQt5 5.15.1
.

Answered By: ALI3N
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.