connect(&thread, &QThread::started, &a, &A::process);
为了方便管理线程,这里最好再写一个管理线程的类,代码如下:
#ifndef AUXCONTROLLER_H
#define AUXCONTROLLER_H
#include "auxThreadMotor.h"
#include "auxThreadMPU.h"
class auxThreadController : public QObject
{
Q_OBJECT
public:
auxThreadController()
{
motor.moveToThread(&motorThread);
mpu.moveToThread(&MPUThread);
//启动线程
motorThread.start();
MPUThread.start();
connect(&motorThread, &QThread::started, &motor, &auxThreadMotor::process);
connect(&MPUThread, &QThread::started, &mpu, &auxThreadMPU::process);
//该线程结束时销毁
connect(&motorThread, &QThread::finished, &motor, &QObject::deleteLater);
connect(&MPUThread, &QThread::finished, &mpu, &QObject::deleteLater);
}
//析构函数中调用quit()函数结束线程
~auxThreadController()
{
motorThread.quit();
motorThread.wait();
MPUThread.quit();
MPUThread.wait();
}
private:
QThread motorThread;
QThread MPUThread;
auxThreadMotor motor;
auxThreadMPU mpu;
};
#endif // !AUXCONTROLLER_H
使用的时候直接在main函数里实例化auxThreadController的一个对象即可。