理解QT线程中的moveToThread


             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。定时器不能再构造函数里面做,因为构造函数是属于主线程的。




  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt的多线程可以使用`QThread`类来实现,而`moveToThread`是一个很常用的函数,用于将一个QObject对象移动到另一个线程执行。 下面是一个使用`moveToThread`的简单例子: ```cpp #include <QCoreApplication> #include <QThread> #include <QDebug> class Worker : public QObject { Q_OBJECT public: Worker(QObject *parent = nullptr) : QObject(parent) {} public slots: void doWork() { qDebug() << "Worker thread:" << QThread::currentThreadId(); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Worker worker; QThread thread; worker.moveToThread(&thread); QObject::connect(&thread, &QThread::started, &worker, &Worker::doWork); QObject::connect(&worker, &Worker::destroyed, &thread, &QThread::quit); thread.start(); return a.exec(); } ``` 在上面的例子,我们创建了一个`Worker`类,它继承自`QObject`类,并有一个`doWork`槽函数。我们将`worker`对象通过`moveToThread`函数移动到了`thread`线程。然后,我们通过`connect`函数将`thread`的`started`信号连接到`worker`的`doWork`槽函数上,当线程启动时,`doWork`槽函数会在`thread`线程执行。同时,我们还将`worker`的`destroyed`信号连接到`thread`的`quit`槽函数上,以保证线程能够正确退出。 需要注意的是,如果我们将一个QObject对象移动到了另一个线程执行,那么它的所有信号和槽函数都必须在该线程执行,否则会出现问题。所以,在上面的例子,我们将`worker`对象的`doWork`槽函数定义为`public slots`,并且在`thread`线程执行,以保证它能正确执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值