QT 线程中使用子线程操作注意事项

       QT在项目使用过程中创建了一个线程,由于线程中run还是中处理有自己想要处理的业务逻辑,想在启用一个线程或者定时器去做别的事情,如果不使用信号槽的情况下使用线程是不存在问题的

#ifndef MONITORTHREAD_H
#define MONITORTHREAD_H
 
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
 
class WorkerSubThread: public QThread
{
public:
    WorkerSubThread() { }
 
    virtual void run()
    {
        qDebug() << "run thread";
    }
};
 
class MonitorThread : public QThread
{
public:
    MonitorThread();
    ~MonitorThread();
 
protected:
    void run();
 
    WorkerSubThread *m_pSubThread;
 
    QTimer * m_pTimer;
 
//public slots:
//    void ExecThread();
 
private:
    bool m_bStop;
};
 
#endif // MONITORTHREAD_H

#include "MonitorThread.h"
 
MonitorThread::MonitorThread()
{
    m_pSubThread = new WorkerSubThread;
    m_pSubThread->start();
 
    m_pTimer = new QTimer();
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(ExecThread()));
    m_pTimer->start(500);
 
    m_bStop = false;
}
 
MonitorThread::~MonitorThread()
{
    m_bStop = true;
}
 
void MonitorThread::run()
{
    while(!m_bStop)
    {
//        m_pSubThread = new WorkerSubThread;
//        m_pSubThread->run();
        qDebug() << "run bussiness logic";
        //进行相关操作
        msleep(100);
    }
}
 
void MonitorThread::ExecThread()
{
    if (m_pSubThread != 0)
        m_pSubThread->start();
}
如果使用信号槽的话需要添加Q_OBJECT宏用于此时由于将无法使用在线程中使用线程了,会报错误信息,无法连接相关的线程类。解决方法就是不使用定时器和信号槽了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Qt线程可以通过以下方式向线程传递参数: 1. 使用信号与槽机制:在线程定义一个信号,线程定义一个槽函数,线程通过emit发送信号并传递参数。 2. 使用QThread的构造函数:在创建线程的时候,可以在构造函数传递参数。 3. 使用QRunnable:将需要传递的参数打包成一个类,继承自QRunnable,然后将其传递给QThreadPool来执行。 下面是使用信号与槽机制的示例代码: ```cpp // MyThread.h #include <QThread> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QObject *parent = nullptr); signals: void sendData(QString data); protected: void run() override; }; // MyThread.cpp #include "MyThread.h" #include <QDebug> MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() { // 执行线程任务 QString data = "Hello, World!"; // 线程要传递的数据 emit sendData(data); // 发送信号并传递数据 } // MainWindow.h #include <QMainWindow> #include "MyThread.h" class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_recvData(QString data); private: Ui::MainWindow *ui; MyThread *m_thread; }; // MainWindow.cpp #include "MainWindow.h" #include "ui_MainWindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); m_thread = new MyThread(this); connect(m_thread, &MyThread::sendData, this, &MainWindow::on_recvData); // 连接信号与槽函数 } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_recvData(QString data) { qDebug() << "Received data from sub-thread: " << data; } void MainWindow::on_pushButton_clicked() { m_thread->start(); // 启动线程 } ``` 在上面的代码线程通过connect连接了MyThread的sendData信号和MainWindow的on_recvData槽函数,当MyThread发送sendData信号时,线程会自动调用on_recvData槽函数,并将数据作为参数传递进来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值