C

Timer Class

class Qul::Timer

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

Header: #include <Timer>

Public Functions

Timer(int interval = 0)
virtual ~Timer()
int interval() const
bool isActive() const
bool isSingleShot() const
void onTimeout(const Func &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.

[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.

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

Set a functor object as a callback.

The argument f must be a functor 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 functor stays active.

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.

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.

void Timer::stop()

Stops the timer.

Available under certain Qt licenses.
Find out more.