理解QT线程中的moveToThread

本文通过具体示例介绍如何在QT中使用moveToThread方法实现跨线程操作,特别是如何在子线程中启动和停止定时器,避免线程间错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值