Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QSessionManager Class Reference

The QSessionManager class provides access to the session manager. More...

#include <qsessionmanager.h>

+ Inheritance diagram for QSessionManager:
+ Collaboration diagram for QSessionManager:

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.
 
- Public Member Functions inherited from QObject
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< QStringbindableObjectName ()
 
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).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
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::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, 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 (QAnyStringView 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 >
findChild (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.
 
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 QObjectListchildren () 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< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () 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

- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 
- Signals inherited from QObject
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 Public Member Functions inherited from QObject
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)
 
- Protected Member Functions inherited from QObject
QObjectsender () 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)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Detailed Description

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.

See also
QGuiApplication, {Session Management}

Definition at line 22 of file qsessionmanager.h.

Member Enumeration Documentation

◆ RestartHint

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.

Member Function Documentation

◆ allowsErrorInteraction()

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.

See also
allowsInteraction(), release(), cancel()

Definition at line 190 of file qsessionmanager.cpp.

References d.

◆ allowsInteraction()

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:

: QWidget(parent)
{
this, &MyMainWidget::commitData);
}
void MyMainWidget::commitData(QSessionManager& manager)
{
if (manager.allowsInteraction()) {
mainWindow,
tr("My Application"),
tr("Save changes to document?"),
switch (ret) {
manager.release();
if (!saveDocument())
manager.cancel();
break;
break;
default:
manager.cancel();
}
} else {
// we did not get permission to interact, then
// do something reasonable instead
}
}

If an error occurred within the application while saving its data, you may want to try allowsErrorInteraction() instead.

See also
QGuiApplication::commitDataRequest(), release(), cancel()

Definition at line 173 of file qsessionmanager.cpp.

References d.

◆ cancel()

void QSessionManager::cancel ( )

Tells the session manager to cancel the shutdown process.

Applications should not call this function without asking the user first.

See also
allowsInteraction(), allowsErrorInteraction()

Definition at line 214 of file qsessionmanager.cpp.

References d.

◆ discardCommand()

QStringList QSessionManager::discardCommand ( ) const

Returns the currently set discard command.

See also
setDiscardCommand(), restartCommand(), setRestartCommand()

Definition at line 308 of file qsessionmanager.cpp.

References d.

◆ isPhase2()

bool QSessionManager::isPhase2 ( ) const

Returns true if the session manager is currently performing a second session management phase; otherwise returns false.

See also
requestPhase2()

Definition at line 348 of file qsessionmanager.cpp.

References d.

◆ release()

void QSessionManager::release ( )

Releases the session manager's interaction semaphore after an interaction phase.

See also
allowsInteraction(), allowsErrorInteraction()

Definition at line 202 of file qsessionmanager.cpp.

References d.

◆ requestPhase2()

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.

Note
If another application has requested a second phase it may get called before, simultaneously with, or after your application's second phase.
See also
isPhase2()

Definition at line 371 of file qsessionmanager.cpp.

References d.

◆ restartCommand()

QStringList QSessionManager::restartCommand ( ) const

Returns the currently set restart command.

See also
setRestartCommand(), restartHint()

Definition at line 286 of file qsessionmanager.cpp.

References d.

◆ restartHint()

QSessionManager::RestartHint QSessionManager::restartHint ( ) const

Returns the application's current restart hint.

The default is RestartIfRunning.

See also
setRestartHint()

Definition at line 248 of file qsessionmanager.cpp.

References d.

◆ sessionId()

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.

See also
sessionKey(), QGuiApplication::sessionId()

Definition at line 119 of file qsessionmanager.cpp.

References d.

◆ sessionKey()

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().

See also
sessionId(), QGuiApplication::sessionKey()

Definition at line 137 of file qsessionmanager.cpp.

References d.

◆ setDiscardCommand()

void QSessionManager::setDiscardCommand ( const QStringList & command)

Sets the discard command to the given command.

See also
discardCommand(), setRestartCommand()

Definition at line 297 of file qsessionmanager.cpp.

References d.

◆ setManagerProperty() [1/2]

void QSessionManager::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.

The property called name has its value set to the string value.

Definition at line 322 of file qsessionmanager.cpp.

References d.

◆ setManagerProperty() [2/2]

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 335 of file qsessionmanager.cpp.

References d.

◆ setRestartCommand()

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.

See also
restartCommand(), setDiscardCommand(), setRestartHint()

Definition at line 275 of file qsessionmanager.cpp.

References d.

◆ setRestartHint()

void QSessionManager::setRestartHint ( QSessionManager::RestartHint hint)

Sets the application's restart hint to hint.

On application startup, the hint is set to RestartIfRunning.

Note
These flags are only hints, a session manager may or may not respect them.

We recommend setting the restart hint in QGuiApplication::saveStateRequest() because most session managers perform a checkpoint shortly after an application's startup.

See also
restartHint()

Definition at line 234 of file qsessionmanager.cpp.

References d, and hint().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QGuiApplication

friend class QGuiApplication
friend

Definition at line 59 of file qsessionmanager.h.

◆ QGuiApplicationPrivate

friend class QGuiApplicationPrivate
friend

Definition at line 60 of file qsessionmanager.h.


The documentation for this class was generated from the following files: