我生成一个辅助线程来执行一些繁琐的工作,将目标QTextBrowser
传递给它,并希望它可以通过insertHtml()
向QTextBrowser
输出运行时消息。有时它会起作用,但最终会使应用程序崩溃。
最初,我使用print()
,并将stdout和sderr重定向到QTextBrowser
。它运作良好。因为我需要富文本功能所以我放弃了这种方法。
在GUI应用程序中,主线程也称为GUI线程,因为它是唯一允许执行GUI相关操作的线程。
如上所述,每个程序在启动时都有一个线程。这个线程被称为"主线程" (也称为" GUI线程"在Qt应用程序中)。 Qt GUI必须在此线程中运行。所有小部件和几个相关的类,例如QPixmap,都不能在辅助线程中工作
我不确定。我需要有人来证实这一点。并且可以建议实施它的常规方法。
答案 0 :(得分:6)
通常,GUI类(小部件+其他一些)只应在主线程中使用,如您在问题中引用的文档中所述。所以,是的,不要在新的帖子中弄乱QTextBrowser
。
但是你应该注意insertHtml
是一个插槽。因此,您可以轻松地将信号连接到您在不同线程中发出的信号(只是不要明确表示直接连接)。