QDialog - Prevent Closing in Python and PyQt
Asked Answered
P

4

8

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_())
Pablo answered 15/8, 2013 at 15:49 Comment(2)
Can you post some code?Disarming
let me get it off my pi one secondPablo
D
17

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)
Disarming answered 15/8, 2013 at 16:4 Comment(5)
ok thanks but i have one more question if you could update your code on how to keep a user from click out of the box so it would show up as on top and you couldnt click out of it?Pablo
@Pablo I do not have any more time to write the code, but you can achieve what you want by taking a look at pyqt windows flagsDisarming
now im runnig into a problem a button is overiding another button i think im ussing layout.add.Widget(self.closebutton)Pablo
i mean programmaticallyWandering
You can disable them: https://mcmap.net/q/1244717/-qdialog-prevent-closing-in-python-and-pyqtAlphard
A
7

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 .

Alphard answered 3/12, 2020 at 11:54 Comment(2)
The part about "exclude the ones you want to be disabled" was the key for me.Teenyweeny
nice! For PyQt6, use QtCore.Qt.WindowType.WindowMinimizeButtonHintCachalot
C
3

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

Chthonian answered 13/1, 2017 at 19:3 Comment(0)
R
0

Turns out if you are using window.setWindowFlags(), you are explicitly stating which of the buttons you want enabled.

But if you are using window.setWindowFlag(), you are explicitly stating the only one you want available.

setWindowFlag() and setWindowFlags()they are different methods!

Rocaille answered 26/6 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.