![]() |
Qt 6.x
The Qt SDK
|
The QSessionManager class provides access to the session manager. More...
#include <qsessionmanager.h>
Public Types | |
enum | RestartHint { RestartIfRunning , RestartAnyway , RestartImmediately , RestartNever } |
This enum type defines the circumstances under which this application wants to be restarted by the session manager. More... | |
Public Member Functions | |
QString | sessionId () const |
Returns the identifier of the current session. | |
QString | sessionKey () const |
Returns the session key in the current session. | |
bool | allowsInteraction () |
Asks the session manager for permission to interact with the user. | |
bool | allowsErrorInteraction () |
Returns true if error interaction is permitted; otherwise returns false . | |
void | release () |
Releases the session manager's interaction semaphore after an interaction phase. | |
void | cancel () |
Tells the session manager to cancel the shutdown process. | |
void | setRestartHint (RestartHint) |
Sets the application's restart hint to hint. | |
RestartHint | restartHint () const |
Returns the application's current restart hint. | |
void | setRestartCommand (const QStringList &) |
If the session manager is capable of restoring sessions it will execute command in order to restore the application. | |
QStringList | restartCommand () const |
Returns the currently set restart command. | |
void | setDiscardCommand (const QStringList &) |
Sets the discard command to the given command. | |
QStringList | discardCommand () const |
Returns the currently set discard command. | |
void | setManagerProperty (const QString &name, const QString &value) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Low-level write access to the application's identification and state records are kept in the session manager. | |
void | setManagerProperty (const QString &name, const QStringList &value) |
Low-level write access to the application's identification and state record are kept in the session manager. | |
bool | isPhase2 () const |
Returns true if the session manager is currently performing a second session management phase; otherwise returns false . | |
void | requestPhase2 () |
Requests a second session management phase for the application. | |
![]() | |
Q_INVOKABLE | QObject (QObject *parent=nullptr) |
Constructs an object with parent object parent. | |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. | |
virtual bool | event (QEvent *event) |
This virtual function receives events to an object and should return true if the event e was recognized and processed. | |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
Filters events if this object has been installed as an event filter for the watched object. | |
QString | objectName () const |
Q_WEAK_OVERLOAD void | setObjectName (const QString &name) |
Sets the object's name to name. | |
void | setObjectName (QAnyStringView name) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QBindable< QString > | bindableObjectName () |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false . | |
bool | isWindowType () const |
Returns true if the object is a window; otherwise returns false . | |
bool | isQuickItemType () const |
Returns true if the object is a QQuickItem; otherwise returns false . | |
bool | signalsBlocked () const noexcept |
Returns true if signals are blocked; otherwise returns false . | |
bool | blockSignals (bool b) noexcept |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). | |
QThread * | thread () const |
Returns the thread in which the object lives. | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. | |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds. | |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType=Qt::CoarseTimer) |
void | killTimer (int id) |
Kills the timer with timer identifier, id. | |
template<typename T > | |
T | findChild (const QString &aName=QString(), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object. | |
template<typename T > | |
QList< T > | findChildren (const QString &aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. | |
template<typename T > | |
QList< T > | findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
const QObjectList & | children () const |
Returns a list of child objects. | |
void | setParent (QObject *parent) |
Makes the object a child of parent. | |
void | installEventFilter (QObject *filterObj) |
Installs an event filter filterObj on this object. | |
void | removeEventFilter (QObject *obj) |
Removes an event filter object obj from this object. | |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const |
bool | disconnect (const QObject *receiver, const char *member=nullptr) const |
void | dumpObjectTree () const |
Dumps a tree of children to the debug output. | |
void | dumpObjectInfo () const |
Dumps information about signal connections, etc. | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. | |
bool | setProperty (const char *name, QVariant &&value) |
QVariant | property (const char *name) const |
Returns the value of the object's name property. | |
QList< QByteArray > | dynamicPropertyNames () const |
QBindingStorage * | bindingStorage () |
const QBindingStorage * | bindingStorage () const |
QObject * | parent () const |
Returns a pointer to the parent object. | |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false . | |
Friends | |
class | QGuiApplication |
class | QGuiApplicationPrivate |
Additional Inherited Members | |
![]() | |
void | deleteLater () |
\threadsafe | |
![]() | |
void | destroyed (QObject *=nullptr) |
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked. | |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
This signal is emitted after the object's name has been changed. | |
![]() | |
static QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
\threadsafe | |
static QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection) |
template<typename Func1 , typename Func2 > | |
static QMetaObject::Connection | connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
\threadsafe | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static bool | disconnect (const QMetaObject::Connection &) |
Disconnect a connection. | |
template<typename Func1 , typename Func2 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot) |
template<typename Func1 > | |
static bool | disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero) |
![]() | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr. | |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. | |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | timerEvent (QTimerEvent *event) |
This event handler can be reimplemented in a subclass to receive timer events for the object. | |
virtual void | childEvent (QChildEvent *event) |
This event handler can be reimplemented in a subclass to receive child events. | |
virtual void | customEvent (QEvent *event) |
This event handler can be reimplemented in a subclass to receive custom events. | |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
QObject (QObjectPrivate &dd, QObject *parent=nullptr) | |
![]() | |
QScopedPointer< QObjectData > | d_ptr |
![]() | |
QString | objectName |
the name of this object | |
![]() | |
template< class T > T | qobject_cast (const QObject *object) |
Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns \nullptr. | |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
\macro Q_CLASSINFO(Name, Value) | |
The QSessionManager class provides access to the session manager.
\inmodule QtGui
A session manager in a desktop environment (in which Qt GUI applications live) keeps track of a session, which is a group of running applications, each of which has a particular state. The state of an application contains (most notably) the documents the application has open and the position and size of its windows.
The session manager is used to save the session, e.g., when the machine is shut down, and to restore a session, e.g., when the machine is started up. We recommend that you use QSettings to save an application's settings, for example, window positions, recently used files, etc. When the application is restarted by the session manager, you can restore the settings.
QSessionManager provides an interface between the application and the platform's session manager. In Qt, session management requests for action are handled by the two signals QGuiApplication::commitDataRequest() and QGuiApplication::saveStateRequest(). Both provide a reference to a QSessionManager object as argument. The session manager can only be accessed in slots invoked by these signals.
No user interaction is possible unless the application gets explicit permission from the session manager. You ask for permission by calling allowsInteraction() or, if it is really urgent, allowsErrorInteraction(). Qt does not enforce this, but the session manager may.
You can try to abort the shutdown process by calling cancel().
For sophisticated session managers provided on Unix/X11, QSessionManager offers further possibilities to fine-tune an application's session management behavior: setRestartCommand(), setDiscardCommand(), setRestartHint(), setProperty(), requestPhase2(). See the respective function descriptions for further details.
Definition at line 22 of file qsessionmanager.h.
This enum type defines the circumstances under which this application wants to be restarted by the session manager.
The current values are:
\value RestartIfRunning If the application is still running when the session is shut down, it wants to be restarted at the start of the next session.
\value RestartAnyway The application wants to be started at the start of the next session, no matter what. (This is useful for utilities that run just after startup and then quit.)
\value RestartImmediately The application wants to be started immediately whenever it is not running.
\value RestartNever The application does not want to be restarted automatically.
The default hint is RestartIfRunning
.
Enumerator | |
---|---|
RestartIfRunning | |
RestartAnyway | |
RestartImmediately | |
RestartNever |
Definition at line 38 of file qsessionmanager.h.
bool QSessionManager::allowsErrorInteraction | ( | ) |
Returns true
if error interaction is permitted; otherwise returns false
.
This is similar to allowsInteraction(), but also enables the application to tell the user about any errors that occur. Session managers may give error interaction requests higher priority, which means that it is more likely that an error interaction is permitted. However, you are still not guaranteed that the session manager will allow interaction.
Definition at line 190 of file qsessionmanager.cpp.
References d.
bool QSessionManager::allowsInteraction | ( | ) |
Asks the session manager for permission to interact with the user.
Returns true if interaction is permitted; otherwise returns false
.
The rationale behind this mechanism is to make it possible to synchronize user interaction during a shutdown. Advanced session managers may ask all applications simultaneously to commit their data, resulting in a much faster shutdown.
When the interaction is completed we strongly recommend releasing the user interaction semaphore with a call to release(). This way, other applications may get the chance to interact with the user while your application is still busy saving data. (The semaphore is implicitly released when the application exits.)
If the user decides to cancel the shutdown process during the interaction phase, you must tell the session manager that this has happened by calling cancel().
Here's an example of how an application's QGuiApplication::commitDataRequest() might be implemented:
If an error occurred within the application while saving its data, you may want to try allowsErrorInteraction() instead.
Definition at line 173 of file qsessionmanager.cpp.
References d.
void QSessionManager::cancel | ( | ) |
Tells the session manager to cancel the shutdown process.
Applications should not call this function without asking the user first.
Definition at line 214 of file qsessionmanager.cpp.
References d.
QStringList QSessionManager::discardCommand | ( | ) | const |
Returns the currently set discard command.
To iterate over the list, you can use the \l foreach pseudo-keyword:
Definition at line 316 of file qsessionmanager.cpp.
References d.
bool QSessionManager::isPhase2 | ( | ) | const |
Returns true
if the session manager is currently performing a second session management phase; otherwise returns false
.
Definition at line 356 of file qsessionmanager.cpp.
References d.
void QSessionManager::release | ( | ) |
Releases the session manager's interaction semaphore after an interaction phase.
Definition at line 202 of file qsessionmanager.cpp.
References d.
void QSessionManager::requestPhase2 | ( | ) |
Requests a second session management phase for the application.
The application may then return immediately from the QGuiApplication::commitDataRequest() or QApplication::saveStateRequest() function, and they will be called again once most or all other applications have finished their session management.
The two phases are useful for applications such as the X11 window manager that need to store information about another application's windows and therefore have to wait until these applications have completed their respective session management tasks.
Definition at line 379 of file qsessionmanager.cpp.
References d.
QStringList QSessionManager::restartCommand | ( | ) | const |
Returns the currently set restart command.
To iterate over the list, you can use the \l foreach pseudo-keyword:
Definition at line 290 of file qsessionmanager.cpp.
References d.
QSessionManager::RestartHint QSessionManager::restartHint | ( | ) | const |
Returns the application's current restart hint.
The default is RestartIfRunning
.
Definition at line 248 of file qsessionmanager.cpp.
References d.
QString QSessionManager::sessionId | ( | ) | const |
Returns the identifier of the current session.
If the application has been restored from an earlier session, this identifier is the same as it was in the earlier session.
Definition at line 119 of file qsessionmanager.cpp.
References d.
QString QSessionManager::sessionKey | ( | ) | const |
Returns the session key in the current session.
If the application has been restored from an earlier session, this key is the same as it was when the previous session ended.
The session key changes with every call of commitData() or saveState().
Definition at line 137 of file qsessionmanager.cpp.
References d.
void QSessionManager::setDiscardCommand | ( | const QStringList & | command | ) |
Sets the discard command to the given command.
Definition at line 301 of file qsessionmanager.cpp.
References d.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Low-level write access to the application's identification and state records are kept in the session manager.
The property called name has its value set to the string value.
Definition at line 330 of file qsessionmanager.cpp.
References d.
void QSessionManager::setManagerProperty | ( | const QString & | name, |
const QStringList & | value | ||
) |
Low-level write access to the application's identification and state record are kept in the session manager.
The property called name has its value set to the string list value.
Definition at line 343 of file qsessionmanager.cpp.
References d.
void QSessionManager::setRestartCommand | ( | const QStringList & | command | ) |
If the session manager is capable of restoring sessions it will execute command in order to restore the application.
The command defaults to
The -session
option is mandatory; otherwise QGuiApplication cannot tell whether it has been restored or what the current session identifier is. See QGuiApplication::isSessionRestored() and QGuiApplication::sessionId() for details.
If your application is very simple, it may be possible to store the entire application state in additional command line options. This is usually a very bad idea because command lines are often limited to a few hundred bytes. Instead, use QSettings, temporary files, or a database for this purpose. By marking the data with the unique sessionId(), you will be able to restore the application in a future session.
Definition at line 275 of file qsessionmanager.cpp.
References d.
void QSessionManager::setRestartHint | ( | QSessionManager::RestartHint | hint | ) |
Sets the application's restart hint to hint.
On application startup, the hint is set to RestartIfRunning
.
We recommend setting the restart hint in QGuiApplication::saveStateRequest() because most session managers perform a checkpoint shortly after an application's startup.
Definition at line 234 of file qsessionmanager.cpp.
|
friend |
Definition at line 59 of file qsessionmanager.h.
|
friend |
Definition at line 60 of file qsessionmanager.h.