gpt4 book ai didi

c++ - QThread 和 GUI 线程说明

转载 作者:行者123 更新时间:2023-11-28 00:35:52 26 4
gpt4 key购买 nike

在官方Qt文档中:

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

现在,在一个 qt 项目中,我尝试了以下代码:

QThread* thread = new QThread;
DetectList *list = new DetectList;
list->moveToThread(thread);

connect(thread, SIGNAL(started()), list, SLOT(process()));
thread->start();

其中DetectList是QWidget派生的类。为什么代码编译运行? DetectList 不是必须只在主线程中运行吗?

最佳答案

就像 Laszlo Papp 指出的那样,您正在收到警告,而 moveToThread 无效。战战将说:
QObject::moveToThread:无法将小部件移动到新线程

参见 source code of moveToThread .

我建议您描述您到底想做什么以及为什么需要线程。我很确定有更好的解决方案(比如 Qt Concurrent)。

关于c++ - QThread 和 GUI 线程说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20969332/

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