Qt中的C++线程连接
C++ thread connect in Qt
我开始在Qt中学习C++11标准中的线程。我不能包括库,没有这样的目录。例如,我有以下简单的代码:
#include <QCoreApplication>
#include <thread>
#include <iostream>
using namespace std;
void func();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread th1;
th1.run();
return a.exec();
}
void func()
{
cout << "This string from thread!"<<endl;
}
在代码的第二个字符串上,我有一个错误。编译器没有"看到",我知道我必须"包括"11标准,所以我转到.pro并粘贴CONFIG+=c++11,但这对我没有帮助:c求你了,我需要你的帮助!
您尝试使用QThread
子类,但您说过要使用C++11
thread
,所以请使用以下内容:
#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>
void foo()
{
std::cout << "This string from thread!"<<endl;
//while(true)
//{
// qDebug() <<"works";
// Sleep(500);
//}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::thread t(foo);
t.join();
return a.exec();
}
下一个也是错误的:
MyThread th1;
th1.run();
应为:
MyThread th1;
th1.start();
有关Qt类线程的详细信息:
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
http://qt-project.org/doc/qt-5/thread-basics.html
http://qt-project.org/doc/qt-5/qtconcurrent-index.html
相关文章:
- 提升 ASIO - io_service 不要等待连接到线程
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 如果两个线程相互依赖,则 cpp 线程连接应使用连接导致死锁
- 视频在唤醒其他线程时输入设备断开连接
- 正在连接的等待条件变量的线程会发生什么情况?
- 如何强制线程轮流连接到字符串?
- 将信号从工作线程类连接到控制器类 - QThreads
- 多个网络连接的线程与异步 I/O
- 什么时候标准::线程::连接会因no_such_process而失败
- C++线程的向量在连接时随机崩溃
- Qt的事件循环线程是安全的还是原子的?处理"队列连接"时如何同步?
- 从第二个线程调用Qt信号有效 - >对连接的插槽没有影响
- C++线程在完成执行后仍然"可连接()"?
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- winsock连接调用崩溃如果多个线程运行,则在一个线程中正常工作
- 矢量中的线程无法连接
- C++线程连接会立即返回或不返回
- 如何与多个线程正确连接Wininet
- QT对象::连接:没有此类插槽信号到线程插槽
- 如何将信号从线程连接到插槽