xtlc
xtlc

Reputation: 1378

PyQt5 Make QDialog wait for a process to finish before allowing user input

After searching for some time and finding many results for PyQt4 I was not able to convert myself, I need some help for a status window my application needs.

The window is opened when a process starts and should not allow any further input by the user in the main GUI, also users should not be able to close it until the process is finished and a close button is activated.

I tried this with a QDialog and omitting the frame (still need to catch 'ESC' key) thus far, but I am convinced there is a better solution. My code:

def resultWindow(self):
    self.resultBox = QDialog(self)
    self.resultBox.setWindowTitle("Please Wait")

    self.OkButton = QtWidgets.QPushButton(self.resultBox)
    self.OkButton.setText("Ok")
    self.OkButton.setEnabled(False)
    self.OkButton.clicked.connect(self.OkButton_clkd)
    self.resultBox.setWindowFlags(QtCore.Qt.CustomizeWindowHint | QtCore.Qt.FramelessWindowHint | QtCore.Qt.Tool)
    self.resultBox.exec_()

def OkButton_clkd(self):
    self.resultBox.close()

So, what is the smarter way to do it?

Upvotes: 0

Views: 992

Answers (1)

Heike
Heike

Reputation: 24430

Instead of removing the frame altogether, which also prevents the user from moving or resizing the dialog, you could just remove the close button from the title bar by doing something like

self.resultBox.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.WindowStaysOnTopHint| 
                              QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint)

To catch the escape key for the dialog you can install an eventfilter, e.g.

def resultWindow(self):
    self.resultBox = QtWidgets.QDialog(self)
    self.resultBox.installEventFilter(self)
    ....

def eventFilter(self, object, event):
    if (object == self.resultBox and 
            event.type() == QtCore.QEvent.KeyPress and 
            event.key() == Qt.Key_Escape):
        return True
    return super().eventFilter(object, event)

or you could subclass QDialog and override keyPressEvent()

Upvotes: 1

Related Questions