QTimer::singleShot的作⽤
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";
}
();
}
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();
set(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>
timeout on t2 timer
#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()));
<();
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(); }
athread.h
#ifndef ATHREAD_H
#define ATHREAD_H
#include <QThread>
template<typename T>
class AThread : public QThread
{
public:
explicit AThread(QObject *parent = Q_NULLPTR)
:QThread(parent),m_pt(nullptr){}
~AThread(){}
void bind(T* pt)
{
m_pt = pt;
m_pt->moveToThread(this);
}
protected:
virtual void run()
{
if(nullptr==m_pt) return ;
m_pt->initInThread();
exec();
m_pt->deinitInThread();
}
public:
T* m_pt;
};
#endif // ATHREAD_H

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。