理解QT线程中的moveToThread
2014-10-31 16:58
816 查看
QT产生的线程实际上是从run函数开始的,你继承QThread的类的构造函数里面做的事情其实还是属于主线程做的事情。
当我们想让更多的事情让线程去工作,而run函数里面没有办法满足要求的时候,就要用到moveToThread的方法,将你要做的事情放到别的线程去做,这时候就不局限与只有在run里面。
下面看一个实际的例子:
从上面的代码的运行效果可以看出,test的构造函数式主线程处理,而timerout是子线程处理
上面的例子定时器的new是从主线程发过来的信号后new,这样就是代表定时器是在子线程new,这样子线程才能处理timerout函数,否则会报:QObject::killTimer: timers cannot be stopped from another thread。定时器不能再构造函数里面做,因为构造函数是属于主线程的。
当我们想让更多的事情让线程去工作,而run函数里面没有办法满足要求的时候,就要用到moveToThread的方法,将你要做的事情放到别的线程去做,这时候就不局限与只有在run里面。
下面看一个实际的例子:
//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include "ui_mainwindow.h" #include <QWidget> #include <QThread> #include "test.h" class MainWindow : public QMainWindow, private Ui::MainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); public: test *test_w; public: QThread * thread; signals: void newTimerSig(); }; #endif // MAINWINDOW_H //mainwindow.cpp #include "mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setupUi(this); test_w = new test(); thread = new QThread; test_w->moveToThread (thread); thread->start (); qDebug() << "main Thread id "<< QThread::currentThreadId (); connect(this,SIGNAL(newTimerSig()),test_w,SLOT(new_timer())); emit newTimerSig(); } //test.h #ifndef TEST_H #define TEST_H #include <QObject> #include <QTimer> class test : public QObject { Q_OBJECT public: explicit test(QObject *parent = 0); QTimer *timer; public slots: void timer_out(); void new_timer(); signals: void creadTimer(); void timer_compelet(); void stop_timer(); }; #endif // TEST_H //test.cpp #include "test.h" #include <QDebug> #include <QThread> test::test(QObject *parent) : QObject(parent) { qDebug() << "test struct thread id "<< QThread::currentThreadId (); // connect(this,SIGNAL(creadTimer()),this,SLOT(new_timer())); // emit creadTimer(); } void test::timer_out () { qDebug() << "time out " << QThread::currentThreadId (); timer->stop (); } void test::new_timer() { qDebug() << "connect_slots " << QThread::currentThreadId (); timer = new QTimer(); timer->setInterval (2000); timer->start(); connect(timer,SIGNAL(timeout()),this,SLOT(timer_out())); }
从上面的代码的运行效果可以看出,test的构造函数式主线程处理,而timerout是子线程处理
上面的例子定时器的new是从主线程发过来的信号后new,这样就是代表定时器是在子线程new,这样子线程才能处理timerout函数,否则会报:QObject::killTimer: timers cannot be stopped from another thread。定时器不能再构造函数里面做,因为构造函数是属于主线程的。
相关文章推荐
- Qt 线程 ---movetothread
- 在Qt使用moveToThread() qt的线程
- moveToThread的最简单用法(依葫芦画瓢即可)(使得线程也更偏向于信号槽的使用方法)
- Qt moveToThread
- Qt moveToThread使用说明
- 【Qt】QThread::moveToThread
- 线程使用方法之moveToThread
- Qt ------ Thread Affinity (QObject::moveToThread: Cannot move objects with a parent)
- Qt moveToThread
- moveToThread的最简单用法(依葫芦画瓢即可)(使得线程也更偏向于信号槽的使用方法)
- Qt多线程两种方式优劣,继承QThread还是MoveToThread
- 在Qt使用moveToThread()
- qt thread with movetothread
- moveToThread的最简单用法(依葫芦画瓢即可)(使得线程也更偏向于信号槽的使用方法)
- Qt 多线程之MoveToThread()
- Qt4.7中,线程,信号,事件的一点理解
- 线程ava.lang.OutOfMemoryError: unable to create new native thread
- CCMoveTo的理解
- Java中OutOfMemoryError与unable to create new native thread(JVM创建大量线程)的关系
- 一种使用QThread线程的新方法QObject::moveToThread