是否允许使用辅助线程来执行QTextBrowser.insertHtml()?

时间:2016-06-16 12:03:07

标签: qt pyqt qt4 qt5

我生成一个辅助线程来执行一些繁琐的工作,将目标QTextBrowser传递给它,并希望它可以通过insertHtml()QTextBrowser输出运行时消息。有时它会起作用,但最终会使应用程序崩溃。

最初,我使用print(),并将stdout和sderr重定向到QTextBrowser。它运作良好。因为我需要富文本功能所以我放弃了这种方法。

herehere或此处,我注意到它可能不合法。

  

在GUI应用程序中,主线程也称为GUI线程,因为它是唯一允许执行GUI相关操作的线程。

     

如上所述,每个程序在启动时都有一个线程。这个线程被称为"主线程" (也称为" GUI线程"在Qt应用程序中)。 Qt GUI必须在此线程中运行。所有小部件和几个相关的类,例如QPixmap,都不能在辅助线程中工作

我不确定。我需要有人来证实这一点。并且可以建议实施它的常规方法。

1 个答案:

答案 0 :(得分:6)

通常,GUI类(小部件+其他一些)只应在主线程中使用,如您在问题中引用的文档中所述。所以,是的,不要在新的帖子中弄乱QTextBrowser

文档还说明了Qt classes are only documented as thread-safe if they are intended to be used by multiple threads. If a function is not marked as thread-safe or reentrant, it should not be used from different threads. If a class is not marked as thread-safe or reentrant then a specific instance of that class should not be accessed from different threads

但是你应该注意insertHtml是一个插槽。因此,您可以轻松地将信号连接到您在不同线程中发出的信号(只是不要明确表示直接连接)。