QTimer::singleShot的作用
1.用作单次定时启动某类函数
2.线程操作,主线程操作某个线程类,为了让主线程调用某类接口是子线程里去执行的,可以在调用接口使用QTimer::singleShot去调用想让子线程运行的接口(在调用接口前,必须是该类线程已经movethread)
例子:
main.cpp
#include <QCoreApplication>
#include "amainwork.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
{
AMainWork mainwork;
mainwork.init();
QThread::sleep(10);
qDebug() << "main";
}
return a.exec();
}
amainwork
#ifndef AMAINWORK_H
#define AMAINWORK_H
#include "athreadtestobj.h"
#include <QObject>
#include <QScopedPointer>
class AMainWork : public QObject
{
Q_OBJECT
public:
explicit AMainWork(QObject *parent = 0);
~AMainWork();
void init();
signals:
public slots:
void slotInit();
private:
QScopedPointer<AThreadTestObj> m_threadTestObj;
};
#endif // AMAINWORK_H
#include "amainwork.h"
#include <QTimer>
#include <QDebug>
#include <QThread>
AMainWork::AMainWork(QObject *parent) : QObject(parent)
{
}
AMainWork::~AMainWork()
{
m_threadTestObj->deinit();
}
void AMainWork::init()
{
QTimer::singleShot(0,this,SLOT(slotInit()));
qDebug() << "init " << QThread::currentThread();
m_threadTestObj.reset(new AThreadTestObj());
m_threadTestObj->init();
m_threadTestObj->start();
}
void AMainWork::slotInit()
{
qDebug() << "slotinit " << QThread::currentThread();
}
athreadtestobj
#ifndef ATHREADTESTOBJ_H
#define ATHREADTESTOBJ_H
#include "athread.h"
#include <QObject>
#include <QTimer>
#include <QThread>
#include <QTimer>
class AThreadTestObj : public QObject
{
Q_OBJECT
public:
explicit AThreadTestObj(QObject *parent = 0);
virtual void init();
virtual void deinit();
virtual bool start();
virtual bool stop();
protected:
virtual void initInThread();
virtual void deinitInThread();
signals:
void sigOndeinit();
public slots:
void sltInit();
void sltDeinit();
void sltOnTimer();
private:
AThread<AThreadTestObj> m_thread;
friend class AThread<AThreadTestObj>;
QTimer * m_pTimer;
};
#endif // ATHREADTESTOBJ_H
#include "athreadtestobj.h"
#include <QDebug>
#include <QEventLoop>
AThreadTestObj::AThreadTestObj(QObject *parent) : QObject(parent)
{
}
void AThreadTestObj::init()
{
m_thread.setObjectName("AThreadTestObj");
m_thread.bind(this);
}
void AThreadTestObj::deinit()
{
stop();
}
bool AThreadTestObj::start()
{
QTimer::singleShot(0,this,SLOT(sltInit()));
m_thread.start();
qDebug() << "AThreadTestObj::start " << QThread::currentThread();
return true;
}
bool AThreadTestObj::stop()
{
qDebug() << "AThreadTestObj::stop " << QThread::currentThread();
QEventLoop loop;
connect(this, SIGNAL(sigOndeinit()), &loop, SLOT(quit()));
QTimer::singleShot(0, this, SLOT(sltDeinit()));
loop.exec();
m_thread.quit();
return true;
}
void AThreadTestObj::initInThread()
{
qDebug() << "AThreadTestObj::initInThread " << QThread::currentThread();
}
void AThreadTestObj::deinitInThread()
{
qDebug() << "AThreadTestObj::deinitInThread " << QThread::currentThread();
}
void AThreadTestObj::sltInit()
{
m_pTimer = new QTimer(this);
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(sltOnTimer()));
m_pTimer->start(600);
}
void AThreadTestObj::sltDeinit()
{
qDebug() << "AThreadTestObj::sltDeinit " << QThread::currentThread();
if(nullptr != m_pTimer)
{
m_pTimer->stop();
delete m_pTimer;
m_pTimer = nullptr;
}
emit sigOndeinit();
}
void AThreadTestObj::sltOnTimer()
{
qDebug() << "AThreadTestObj::sltOnTimer " << QThread::currentThread();
}