Timer Class

class Qul ::Timer

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

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

This class was introduced in Qt Quick Ultralite 1.0.

公共函数

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

详细描述

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

成员函数文档编制

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

另请参阅 setInterval .

bool Timer:: isActive () const

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

bool Timer:: isSingleShot () const

返回 True for single shot timers or False 否则。

另请参阅 setSingleShot .

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

Set a callable object as a callback.

自变量 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.

另请参阅 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.

另请参阅 interval .

void Timer:: setSingleShot ( bool singleShot )

Set whether the timer is a singleShot timer.

单发计时器仅激发一次,非单发定时器被激发每隔 interval 毫秒。

默认值为 False .

另请参阅 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.

另请参阅 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.

另请参阅 stop .

void Timer:: stop ()

停止计时器。

另请参阅 start .