PySide --do something in the background while displaying a modeless dialog

If you want to display a modeless dialog in PySide, use the show method, If some processing is ongoing at the caller The dialog will wait until the process is complete.

To prevent that, regularly in a modeless dialog You need to call QtGui.QApplication.processEvents (). (QProgressbarDialog etc. also call processEvents in the setValue method The dialog is updated regularly)

Below, a modeless dialog that says "Please wait ..." when performing time-consuming processing Is an example of displaying.

py::waitdialog.py


import sys
from PySide import QtGui, QtCore


class WaitDialog(QtGui.QDialog):

    def __init__(self, parent=None):
        super(WaitDialog, self).__init__(parent)

        label1 = QtGui.QLabel("Please wait...")

        layout = QtGui.QVBoxLayout()
        layout.addWidget(label1)
        self.setLayout(layout)

        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowTitle("Work in progress")
        self.resize(200, 100)
        self.setModal(True)
        self.show()
        self.raise_()
        QtGui.QApplication.processEvents()


def main():
    app = QtGui.QApplication(sys.argv)
    widget = WaitDialog()
    u"""Time-consuming process start"""
    import time
    time.sleep(10)
    u"""Time-consuming processing end"""
    widget.close()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Recommended Posts

PySide --do something in the background while displaying a modeless dialog
How to specify a .ui file in the dialog / widget GUI in PySide
Do something like a Python interpreter in Visual Studio Code
Do something object-oriented in GO language
A useful note when using Python for the first time in a while