gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:31:56 25 4
gpt4 key购买 nike

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

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

来自herehere或者在这里,我注意到它可能不合法。

In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations.

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary thread

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

最佳答案

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

关于qt - 是否允许使用辅助线程执行 QTextBrowser.insertHtml() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37858645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com