【问题标题】:Is it allowed to use secondary thread to do QTextBrowser.insertHtml()?是否允许使用辅助线程来执行 QTextBrowser.insertHtml()?
【发布时间】:2016-10-17 22:31:37
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: qt pyqt qt4 qt5


    【解决方案1】:

    一般来说,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 是一个插槽。因此,您可以轻松地将在不同线程中发出的信号连接到它(只是不要将其明确设为直接连接)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多