- Qt 6.10
- Qt Core
- C++ Classes
- QBasicTimer
- Qt 6.10.2 Reference Documentation

# QBasicTimer Class

The QBasicTimer class provides timer events for objects. More...

Header: | `#include <QBasicTimer>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- Deprecated members
- QBasicTimer is part of Event Classes.

## Public Types

| Duration

## Public Functions

| QBasicTimer()
| QBasicTimer(QBasicTimer &&other)
| ~QBasicTimer()
`(since 6.8)` Qt::TimerId | id() const
bool | isActive() const
`(since 6.5)` void | start(QBasicTimer::Duration duration, QObject *object)
`(since 6.5)` void | start(QBasicTimer::Duration duration, Qt::TimerType timerType, QObject *obj)
void | stop()
void | swap(QBasicTimer &other)
QBasicTimer & | operator=(QBasicTimer &&other)

## Related Non-Members

void | swap(QBasicTimer &lhs, QBasicTimer &rhs)

## Detailed Description

This is a fast, lightweight, and low-level class used by Qt internally. We recommend using the higher-level QTimer class rather than this class if you want to use timers in your applications. Note that this timer is a repeating timer that will send subsequent timer events unless the stop() function is called.

To use this class, create a QBasicTimer, and call its start() function with a timeout interval and with a pointer to a QObject subclass. When the timer times out it will send a timer event to the QObject subclass. The timer can be stopped at any time using stop(). isActive() returns `true` for a timer that is running; i.e. it has been started, has not reached the timeout time, and has not been stopped. The timer's ID can be retrieved using timerId().

Objects of this class cannot be copied, but can be moved, so you can maintain a list of basic timers by holding them in container that supports move-only types, e.g. std::vector.

See also QTimer, QChronoTimer, QTimerEvent, QObject::timerEvent(), Timers, and Affine Transformations.

## Member Type Documentation

### `[alias]` QBasicTimer::Duration

A `std::chrono::duration` type that is used in various API in this class. This type exists to facilitate a possible transition to a higher or lower granularity.

In all current platforms, it is `nanoseconds`.

## Member Function Documentation

### `[constexpr noexcept]` QBasicTimer::QBasicTimer()

Constructs a basic timer.

See also start().

### `[noexcept]` QBasicTimer::QBasicTimer(QBasicTimer &&other)

Move-constructs a basic timer from other, which is left inactive.

See also isActive() and swap().

### `[noexcept]` QBasicTimer::~QBasicTimer()

Destroys the basic timer.

### `[noexcept, since 6.8]` Qt::TimerId QBasicTimer::id() const

Returns the timer's ID.

This function was introduced in Qt 6.8.

See also QTimerEvent::id().

### `[noexcept]` bool QBasicTimer::isActive() const

Returns `true` if the timer is running and has not been stopped; otherwise returns `false`.

See also start() and stop().

### `[since 6.5]` void QBasicTimer::start(QBasicTimer::Duration duration, QObject *object)

Starts (or restarts) the timer with a duration timeout. The timer will be a Qt::CoarseTimer. See Qt::TimerType for information on the different timer types.

The given object will receive timer events.

Starting from Qt 6.10, setting a negative interval will result in a run-time warning and the value being reset to 1ms. Before Qt 6.10 a Qt Timer would let you set a negative interval but behave in surprising ways (for example stop the timer if it was running or not start it at all).

Note: Starting from Qt 6.9 this method takes std::chrono::nanoseconds, before that it took std::chrono::milliseconds. This change is backwards compatible.

This function was introduced in Qt 6.5.

See also stop(), isActive(), QObject::timerEvent(), and Qt::CoarseTimer.

### `[since 6.5]` void QBasicTimer::start(QBasicTimer::Duration duration, Qt::TimerType timerType, QObject *obj)

Starts (or restarts) the timer with a duration timeout and the given timerType. See Qt::TimerType for information on the different timer types.

Starting from Qt 6.10, setting a negative interval will result in a run-time warning and the value being reset to 1ms. Before Qt 6.10 a Qt Timer would let you set a negative interval but behave in surprising ways (for example stop the timer if it was running or not start it at all).

obj will receive timer events.

Note: Starting from Qt 6.9 this method takes std::chrono::nanoseconds, before that it took std::chrono::milliseconds. This change is backwards compatible.

This is an overloaded function.

This function was introduced in Qt 6.5.

See also stop(), isActive(), QObject::timerEvent(), and Qt::TimerType.

### void QBasicTimer::stop()

Stops the timer.

See also start() and isActive().

### `[noexcept]` void QBasicTimer::swap(QBasicTimer &other)

Swaps this timer with other. This operation is very fast and never fails.

### `[noexcept]` QBasicTimer &QBasicTimer::operator=(QBasicTimer &&other)

Move-assigns other to this basic timer. The timer previously represented by this basic timer is stopped. other is left as inactive.

See also stop(), isActive(), and swap().

## Related Non-Members

### `[noexcept]` void swap(QBasicTimer &lhs, QBasicTimer &rhs)

Swaps the timer lhs with rhs. This operation is very fast and never fails.