如何在没有用户交互的情况下显示模式 QDialog?

how to show a modal QDialog without user interaction?

我想展示一个无框模式对话框,它不提供任何交互,甚至不提供关闭对话框的功能。这个想法是打开对话框显示一条消息警告正在进行的长时间操作,运行 该操作,而不是关闭对话框。

qt 文档似乎表明可以在不执行事件循环的情况下显示模态对话框:https://doc.qt.io/qt-5/qdialog.html#modal-dialogs

但是当我这样做时,对话框永远无法正确呈现在屏幕上。我得到一个黑色小部件,它的标签仍然不可见。

这是我的尝试:

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()

您不必使用 processEvents,而是在另一个线程中实现任务,在这种情况下,我创建了一个 QObject,它存在于另一个线程中,并在任务结束时发出一个信号,用于关闭 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_())