KIdleTime Class

KIdleTime is a singleton reporting information on idle time. More...

Header: #include <KIdleTime>
CMake: find_package(KF6 REQUIRED COMPONENTS IdleTime)
target_link_libraries(mytarget PRIVATE KF6::IdleTime)
Since: 4.4
Inherits: QObject

Public Functions

int idleTime() const
QHash<int, int> idleTimeouts() const
void simulateUserActivity()

Public Slots

int addIdleTimeout(int msec)
(since 5.83) int addIdleTimeout(std::chrono::milliseconds msec)
void catchNextResumeEvent()
void removeAllIdleTimeouts()
void removeIdleTimeout(int identifier)
void stopCatchingResumeEvent()

Signals

void resumingFromIdle()
void timeoutReached(int identifier, int msec)

Static Public Members

KIdleTime *instance()

Detailed Description

It is useful not only for finding out about the current idle time of the PC, but also for getting notified upon idle time events, such as custom timeouts, or user activity.

Note: All the intervals and times in this library are in milliseconds, unless specified otherwise.

Member Function Documentation

[slot] int KIdleTime::addIdleTimeout(int msec)

Adds a new timeout to catch. When calling this method, after the system will be idle for msec milliseconds, the signal timeoutReached() will be triggered. Please note that until you will call removeIdleTimeout() or removeAllIdleTimeouts(), the signal will be triggered every time the system will be idle for msec milliseconds. This function also returns an unique token for the timeout just added to allow easier identification.

msec the time, in milliseconds, after which the signal will be triggered.

Returns an unique identifier for the timeout being added, that will be streamed by timeoutReached().

See also removeIdleTimeout(), removeAllIdleTimeouts(), and timeoutReached().

[slot, since 5.83] int KIdleTime::addIdleTimeout(std::chrono::milliseconds msec)

Convenience overload supporting C++ chrono types. May also be used with chrono literals.

This function was introduced in 5.83.

[slot] void KIdleTime::catchNextResumeEvent()

Catches the next resume from idle event. This means that whenever user activity will be registered, or simulateUserActivity() is called, the signal resumingFromIdle() will be triggered.

Please note that this method will trigger the signal just for the very first resume event after the call: this means you explicitly have to request to track every single resume event you are interested in.

Note: This behavior is due to the fact that a resume event happens whenever the user sends an input to the system. This would lead to a massive amount of signals being delivered when the PC is being used. Moreover, you are usually interested in catching just significant resume events, such as the ones after a significant period of inactivity. For tracking user input, you can use the more efficient methods provided by Qt. The purpose of this library is just monitoring the activity of the user.

See also resumingFromIdle() and simulateUserActivity().

int KIdleTime::idleTime() const

Retrieves the idle time of the system, in milliseconds

QHash<int, int> KIdleTime::idleTimeouts() const

Returns the list of timeout identifiers associated with their duration, in milliseconds, the library is currently listening to.

See also addIdleTimeout(), removeIdleTimeout(), and timeoutReached().

[static] KIdleTime *KIdleTime::instance()

Returns the singleton instance. Use this method to access KIdleTime.

[slot] void KIdleTime::removeAllIdleTimeouts()

Stops catching every set timeout (if any). This means that after calling this method, the signal timeoutReached() won't be called again until you will add another timeout.

See also timeoutReached() and addIdleTimeout().

[slot] void KIdleTime::removeIdleTimeout(int identifier)

Stops catching the idle timeout identified by the token identifier, if it was registered earlier with addIdleTimeout. Otherwise does nothing.

identifier the token returned from addIdleTimeout of the timeout you want to stop listening to

[signal] void KIdleTime::resumingFromIdle()

Triggered, if KIdleTime is catching resume events, when the system resumes from an idle state. This means that either simulateUserActivity() was called or the user sent an input to the system.

See also catchNextResumeEvent().

void KIdleTime::simulateUserActivity()

Attempts to simulate user activity. This implies that after calling this method, the idle time of the system will become 0 and eventually resumingFromIdle() will be triggered.

See also resumingFromIdle().

[slot] void KIdleTime::stopCatchingResumeEvent()

Stops listening for resume event. This function serves for canceling catchNextResumeEvent(), as it will have effect just when catchNextResumeEvent() has been called and resumingFromIdle() not yet triggered.

See also resumingFromIdle() and catchNextResumeEvent().

[signal] void KIdleTime::timeoutReached(int identifier, int msec)

Triggered when the system has been idle for x milliseconds, identified by the previously set timeout.

This signal is triggered whenever each timeout previously registered with addIdleTimeout(int) is reached. It is guaranteed that msec will exactly correspond to the identified timeout.

identifier the identifier of the timeout the system has reached.

msec the time, in milliseconds, the system has been idle for.

See also addIdleTimeout() and removeIdleTimeout().