Skip to content

具有計時功能的線程類別 | A thread class with timer developed with Qt C++ framework by FloatFlower.Huang.

License

Notifications You must be signed in to change notification settings

floatflower/FFQTimerThread

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FFQTimerThread

A thread class with timer developed with Qt C++ framework by FloatFlower.Huang.

Information

Installation

Require Qt5

~$ qmake
~$ make
~$ sudo make install

To link this library to your project, put this to qmake project file.

LIBS += -lffqtimerthread

By default, the library will be installed to /usr/include. To change "/usr" to another location, for example, change to /usr/local/include, run:

~$ qmake -r PREFIX=/usr/local

Usage

Include the headers:

#include <ff/qtimerthread.h>

There are two ways to use QTimerThread, first one is worker-object approach.

These two approach are similar to QThread usage approach.

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = 0) {}
public slots:
    void onTimeout() {
        /* ... do someting when timeout. ... */
    }
};

class Controller : public QObject
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = 0)
    {
        Worker *worker = new Worker;
        m_thread = new FF::QTimerThread;
        m_thread->setInterval(2000);
        worker->moveToThread(m_thread);
        connect(m_thread, SIGNAL(timeout()), worker, SLOT(onTimeout()));
        m_thread->start();
    }

    ~Controller() {
        m_thread->timerQuit();
        m_thread->quit();
        m_thread->wait();
        delete m_thread;
    }
private:
    FF::QTimerThread *m_thread;
};

Second one is to inhreit QTimerThread class and reimplement QTimerThread::doRoutine().

If you are going to use this approach, don't forget to connect signal QTimerThread::timeout() to slot QTimerThread::doRoutine() when subclassing FFQTimerThread.

class WorkerThread : public FF::QTimerThread
{

public:
    explicit WorkerThread() {
        connect(this, SIGNAL(timeout()), this, SLOT(doRoutine()));
    }
    virtual void doRoutine() {
        /* ... do something when timeout. ... */
    }
};


class Controller : public QObject
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = 0)
    {
        m_thread = new WorkThread;
        m_thread->setInterval(2000);
        m_thread->start();
    }

    ~Controller() {
        m_thread->timerQuit();
        m_thread->quit();
        m_thread->wait();
        delete m_thread;
    }
private:
    WorkerThread *m_thread;
};

Notice:

  • Keep in mind that calling QTimerThread::timerQuit() is crucial, because QTimerThread::wait() will wait for the thread for QTimer quit.
  • Instantiating QTimerThread creates two thread pratically, one for timer, the other for doing job.
  • Use QTimerThread::setInterval() to set interval for the timer in QTimerThread, if you don't use this function, interval will be set to 0 msec, otherwise.

About

具有計時功能的線程類別 | A thread class with timer developed with Qt C++ framework by FloatFlower.Huang.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published