在线程中的 Qdialog 上调用 exec() 效果不佳

calling exec() on a Qdialog in a thread doesn't work well

我想在一个线程中调用my_dialog.exec(),但是当主窗口(意思是主线程)处理一个事件时效果不佳,我想知道如何处理这个问题

这是我的测试程序:

import sys
from PyQt5.Qt import *
from threading import Thread
from time import sleep

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(500, 500, 500, 200)

        self.dialog = QDialog(self)
        self.dialog.setGeometry(500, 500, 200, 100)

        btn = QPushButton('click', self)
        btn.clicked.connect(self.show_dialog)

        self.show()

    def show_dialog(self):
        Thread(target=self.execute).start()

    def execute(self):
        sleep(2)
        # keep moving mainwindow untill dialog have shown
        self.dialog.exec_()

app = QApplication(sys.argv)
e = Main()
sys.exit(app.exec_())

当我按下按钮时,它会先休眠两秒钟。 没有主窗口的事件是正常的。 但是当我在两个睡眠秒内继续移动主窗口(或其他事件,如调整大小)时,它们都将变得无响应

不,您不能从另一个线程修改 GUI,请使用信号。

TL;DR;

我在 Qt 中分享一条黄金法则:你不能也不应该从另一个线程修改 GUI。更多信息请阅读:GUI Thread and Worker Thread.

考虑到上述情况,在 Qt 中不同线程中的元素之间进行交互的自然方式是使用信号,因为它们是 thread-safe,如下所示:

class Main(QMainWindow):
    <b>customSignal = pyqtSignal()</b>

    def __init__(self):
        # ...

        self.show()
        <b>self.customSignal.connect(self.dialog.exec_)</b>

    def show_dialog(self):
        Thread(target=self.execute).start()

    def execute(self):
        sleep(2)
        # keep moving mainwindow untill dialog have shown
        <b>self.customSignal.emit()</b>