Timer Class
(Qul::Timer)Header: | #include <Timer> |
Inherits: | Qul::Private::RawTimer (private) |
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() |
- 1 public function inherited from Qul::Private::RawTimer
Additional Inherited Members
- 4 static protected members inherited from Qul::Private::RawTimer
Detailed Description
Provides a way to run repetitive and single-shot timers.
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::Items::QtObject { 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
Getter for the single shot property
void Timer::onTimeout(const Func &f)
Set a functor object as a callback.
The argument need to 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
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 single-shot 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.