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

时间:2019-06-20 09:37:32

标签: python pyqt pyqt5 qdialog

我想显示一个无框架的模态对话框,该对话框不提供任何交互作用,甚至不能关闭该对话框。想法是打开对话框以显示一条消息,警告您正在进行长操作,请运行该操作,然后再关闭对话框。

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

1 个答案:

答案 0 :(得分:3)

您不必使用processEvents,而可以在另一个线程中实现该任务,在这种情况下,我创建了一个QObject,该对象驻留在另一个线程中,并在任务结束时发出信号以关闭窗口。

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