您的位置:首页 > 编程语言 > Qt开发

理解QT线程中的moveToThread

2014-10-31 16:58 816 查看
             QT产生的线程实际上是从run函数开始的,你继承QThread的类的构造函数里面做的事情其实还是属于主线程做的事情。

当我们想让更多的事情让线程去工作,而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。定时器不能再构造函数里面做,因为构造函数是属于主线程的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  moveToThread QThread