• 4427阅读
  • 2回复

[提问]主线程和子线程数据调用,怎么实现? [复制链接]

上一主题 下一主题
离线x3340574
 
只看楼主 倒序阅读 楼主  发表于: 2012-11-16

  1. class Worker : public QObject {

  2.     Q_OBJECT

  3.     

  4. public:

  5.     Worker();

  6.     ~Worker();

  7.     

  8. public slots:

  9.     void process();

  10.     

  11. signals:

  12.     void finished();

  13.     void error(QString err);

  14.     

  15. private:    

  16.     // add your variables here

  17. };

  1. // --- CONSTRUCTOR ---

  2. Worker::Worker() {  

  3.     // you could copy data from constructor arguments to internal variables here.

  4. }



  5. // --- DESTRUCTOR---

  6. Worker::~Worker() {

  7.     // free resources

  8. }



  9. // --- PROCESS ---

  10. // Start processing data.

  11. void Worker::process() {

  12. // allocate resources using new here

  13. qDebug("Hello World!");

  14. emit finished();

  15. }

  1. QThread* thread = new QThread;

  2. Worker* worker = new Worker();

  3. worker->moveToThread(thread);

  4. connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));

  5. connect(thread, SIGNAL(started()), worker, SLOT(process()));

  6. connect(worker, SIGNAL(finished()), thread, SLOT(quit()));

  7. connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));

  8. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

  9. thread->start();

如何利用process()子线程来调用主线程中work构造函数中定义的数据?也就是主线程和子线程的数据调用的问题
离线x3340574
只看该作者 1楼 发表于: 2012-11-16
自己先定一下吧
离线x3340574
只看该作者 2楼 发表于: 2012-11-16
还是没人回答吗?推荐一些具体的资料也好
多线程的数据调用 怎么在QT中实现
快速回复
限100 字节
 
上一个 下一个