【问题标题】:C++ thread connect in QtQt中的C++线程连接
【发布时间】:2014-12-29 09:54:53
【问题描述】:

我开始在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 拜托,我需要你的帮助!

【问题讨论】:

  • 你遇到了什么错误?

标签: c++ qt c++11 boost-thread


【解决方案1】:

你尝试使用QThread子类,但你说你想使用C++11thread所以使用这个:

#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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多