May 2019
Intermediate to advanced
542 pages
13h 37m
English
Being able to delay action in a program is useful in a variety of situations. For example, let's say that we want a modeless pop-up dialog that closes itself after a defined number of seconds rather than waiting for a user to click on a button.
We will start by subclassing QDialog:
class AutoCloseDialog(qtw.QDialog): def __init__(self, parent, title, message, timeout): super().__init__(parent) self.setModal(False) self.setWindowTitle(title) self.setLayout(qtw.QVBoxLayout()) self.layout().addWidget(qtw.QLabel(message)) self.timeout = timeout
Having saved a timeout value, we now want to override the dialog box's show() method so that it closes after that number of seconds.
A naive approach might be as follows:
Read now
Unlock full access