ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

QTimer::singleShot的作用

2020-07-23 14:05:07  阅读:448  来源: 互联网

标签:AMainWork void init QObject AThreadTestObj QTimer singleShot include 作用


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();
}

标签:AMainWork,void,init,QObject,AThreadTestObj,QTimer,singleShot,include,作用
来源: https://www.cnblogs.com/huanyinglvtuan/p/13365918.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有