C

Timer Class

class Qul::Timer

Provides a way to run repetitive and single-shot timers. More...

Header: #include <qul/timer.h>
Since: Qt Quick Ultralite 1.0

Public Functions

Timer(int interval = 0)
virtual ~Timer() override
int interval() const
bool isActive() const
bool isSingleShot() const
void onTimeout(const FuncArg &f)
void setInterval(int msec)
void setSingleShot(bool singleShot)
void start()
void start(int msec)
void stop()

Detailed Description

Example with a C++11 lambda:

Qul::Timer timer;
timer.onTimeout([]() { printf("triggered\n"); });
timer.start(1000); // will trigger every second

Example with a functor:

struct Functor {
    void operator()() { printf("triggered\n"); }
};

Qul::Timer timer;
timer.onTimeout(Functor());
timer.setSingleShot(true);
timer.start(60000); // trigger once in a minute

Example in a QML object:

struct MinuteCounter : Qul::Object
{
    Qul::Property<int> minutes;

    void start() {
        _countMinutes.onTimeout([this]() { minutes.set(minutes.get() + 1); });
        _countMinutes.start(60000);
    }
private:
    Qul::Timer _countMinutes;
};

Member Function Documentation

Timer::Timer(int interval = 0)

Instantiate a new timer.

One can specify the interval (default: 0). The default behavior for timers is to repeat. Use setSingleShot(true) to make it trigger only once.

The timer is not started automatically. Use start() to start it.

[override virtual] Timer::~Timer()

Automatically stops the timer

int Timer::interval() const

Returns the interval

See also setInterval.

bool Timer::isActive() const

Returns true if the timer is running (pending); otherwise returns false

bool Timer::isSingleShot() const

Returns True for single shot timers or False otherwise.

See also setSingleShot.

template <typename FuncArg> void Timer::onTimeout(const FuncArg &f)

Set a callable object as a callback.

The argument f must be a callable object such as a lambda or std::function or any struct with an operator().

It will be called every time the timer triggers.

When calling this function several times, only the last callback stays active.

See also start and setSingleShot.

void Timer::setInterval(int msec)

Sets the timeout interval in milliseconds to msec

Setting the interval of an active timer does not change the next event.

See also interval.

void Timer::setSingleShot(bool singleShot)

Set whether the timer is a singleShot timer.

A single-shot timer fires only once, non-single-shot timers fire every interval milliseconds.

The default value is False.

See also isSingleShot.

void Timer::start()

Starts or restart the timer

If the timer is already running, it will be stopped and restarted.

If singleShot is true, the timer will be activated only once.

See also stop.

void Timer::start(int msec)

Starts or restarts the timer with a timeout interval of msec milliseconds

If the timer is already running, it will be stopped and restarted.

If singleShot is true, the timer will be activated only once.

See also stop.

void Timer::stop()

Stops the timer.

See also start.

Available under certain Qt licenses.
Find out more.