how to show a modal QDialog without user interaction?

Viewed 879

I would like to show a frameless modal dialog that does not provide any interaction, not even the ability to close the dialog. The idea is to open the dialog to display a message warning of a long operation going on, run that operation, than close the dialog.

The qt doc seems to indicate that it is possible to show a modal dialog without executing its event loop: https://doc.qt.io/qt-5/qdialog.html#modal-dialogs

But when I do it, the dialog never properly renders on screen. I get a black widget, and its labels remain invisible.

This is my attempt:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class ModalInfoDialog(QDialog):
    """
    Frameless modal dialog with no interaction
    """

    def __init__(self, text1="Loading project",
                 text2="", parent=None):
        super().__init__(parent)
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
        self.setModal(True)
        self.setStyleSheet(
            """
            QDialog {
                background-color: white;
                border: none;
            }
            """)

        layout = QVBoxLayout(self)
        self.setLayout(layout)

        firstLine = QLabel(text1)
        secondLine = QLabel(text2)

        layout.addWidget(firstLine)
        layout.addWidget(secondLine)


import time
app = QApplication([])

d = ModalInfoDialog("haha!", "huh?")
d.show()
QApplication.processEvents()  # does not help
time.sleep(3)
d.close()
1 Answers

You do not have to use processEvents, instead you implement the task in another thread, in this case I have created a QObject that lives in another thread and emits a signal when the task ends that serves to close the window.

import time
from PyQt5 import QtCore, QtWidgets


class ModalInfoDialog(QtWidgets.QDialog):
    """
    Frameless modal dialog with no interaction
    """

    def __init__(self, text1="Loading project", text2="", parent=None):
        super().__init__(parent)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.FramelessWindowHint)
        self.setModal(True)
        self.setStyleSheet(
            """
            QDialog {
                background-color: white;
                border: none;
            }
            """
        )

        layout = QtWidgets.QVBoxLayout(self)

        firstLine = QtWidgets.QLabel(text1)
        secondLine = QtWidgets.QLabel(text2)

        layout.addWidget(firstLine)
        layout.addWidget(secondLine)


class Worker(QtCore.QObject):
    finished = QtCore.pyqtSignal()

    @QtCore.pyqtSlot()
    def task(self):
        time.sleep(3)
        self.finished.emit()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    d = ModalInfoDialog("haha!", "huh?")
    d.show()
    thread = QtCore.QThread(d)
    worker = Worker()
    worker.finished.connect(d.close)
    worker.moveToThread(thread)
    thread.started.connect(worker.task)
    thread.start()
    sys.exit(app.exec_())
Related