当前位置 博文首页 > 文章内容

    QTimer::singleShot的作用

    作者: 栏目:未分类 时间:2020-07-23 14:03:20

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



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