QSessionManager 类

The QSessionManager class provides access to the session manager. 更多...

头: #include <QSessionManager>
qmake: QT += gui
继承: QObject

公共类型

enum RestartHint { RestartIfRunning, RestartAnyway, RestartImmediately, RestartNever }

公共函数

bool allowsErrorInteraction ()
bool allowsInteraction ()
void cancel ()
QStringList discardCommand () const
bool isPhase2 () const
void release ()
void requestPhase2 ()
QStringList restartCommand () const
QSessionManager::RestartHint restartHint () const
QString sessionId () const
QString sessionKey () const
void setDiscardCommand (const QStringList & 命令 )
void setManagerProperty (const QString & name , const QStringList & value )
void setManagerProperty (const QString & name , const QString & value )
void setRestartCommand (const QStringList & 命令 )
void setRestartHint (QSessionManager::RestartHint hint )

静态公共成员

const QMetaObject staticMetaObject

额外继承成员

详细描述

The QSessionManager class provides access to the session manager.

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

警告: 若使用 QSessionManager , you should disable fallback session management: QGuiApplication::setFallbackSessionManagementEnabled ().

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.

可以试着中止关机进程,通过调用 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.

另请参阅 QGuiApplication and 会话管理 .

成员类型文档编制

enum QSessionManager:: RestartHint

This enum type defines the circumstances under which this application wants to be restarted by the session manager. The current values are:

常量 描述
QSessionManager::RestartIfRunning 0 If the application is still running when the session is shut down, it wants to be restarted at the start of the next session.
QSessionManager::RestartAnyway 1 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.)
QSessionManager::RestartImmediately 2 The application wants to be started immediately whenever it is not running.
QSessionManager::RestartNever 3 The application does not want to be restarted automatically.

默认提示为 RestartIfRunning .

成员函数文档编制

bool QSessionManager:: allowsErrorInteraction ()

返回 true if error interaction is permitted; otherwise returns false .

这类似于 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.

另请参阅 allowsInteraction (), release (),和 cancel ().

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:

MyMainWidget::MyMainWidget(QWidget *parent)
    :QWidget(parent)
{
    QGuiApplication::setFallbackSessionManagementEnabled(false);
    connect(qApp, SIGNAL(commitDataRequest(QSessionManager)), SLOT(commitData(QSessionManager)));
}
void MyMainWidget::commitData(QSessionManager& manager)
{
    if (manager.allowsInteraction()) {
        int ret = QMessageBox::warning(
                    mainWindow,
                    tr("My Application"),
                    tr("Save changes to document?"),
                    QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        switch (ret) {
        case QMessageBox::Save:
            manager.release();
            if (!saveDocument())
                manager.cancel();
            break;
        case QMessageBox::Discard:
            break;
        case QMessageBox::Cancel:
        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 () 代替。

另请参阅 QGuiApplication::commitDataRequest (), release (),和 cancel ().

void QSessionManager:: cancel ()

Tells the session manager to cancel the shutdown process. Applications should not call this function without asking the user first.

另请参阅 allowsInteraction () 和 allowsErrorInteraction ().

QStringList QSessionManager:: discardCommand () const

Returns the currently set discard command.

To iterate over the list, you can use the foreach 伪关键词:

foreach (const QString &command, mySession.discardCommand())
    do_something(command);
					

另请参阅 setDiscardCommand (), restartCommand (),和 setRestartCommand ().

bool QSessionManager:: isPhase2 () const

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

另请参阅 requestPhase2 ().

void QSessionManager:: release ()

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

另请参阅 allowsInteraction () 和 allowsErrorInteraction ().

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.

注意: If another application has requested a second phase it may get called before, simultaneously with, or after your application's second phase.

另请参阅 isPhase2 ().

QStringList QSessionManager:: restartCommand () const

Returns the currently set restart command.

To iterate over the list, you can use the foreach 伪关键词:

foreach (const QString &command, mySession.restartCommand())
    do_something(command);
					

另请参阅 setRestartCommand () 和 restartHint ().

QSessionManager::RestartHint QSessionManager:: restartHint () const

Returns the application's current restart hint. The default is RestartIfRunning .

另请参阅 setRestartHint ().

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.

另请参阅 sessionKey () 和 QGuiApplication::sessionId ().

QString QSessionManager:: sessionKey () const

Returns the session key in the current session.

若应用程序已从早期会话还原,此键与先前会话结束时的键相同。

The session key changes with every call of commitData() or saveState().

另请参阅 sessionId () 和 QGuiApplication::sessionKey ().

void QSessionManager:: setDiscardCommand (const QStringList & 命令 )

Sets the discard command to the given 命令 .

另请参阅 discardCommand () 和 setRestartCommand ().

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 .

void QSessionManager:: setManagerProperty (const QString & name , const QString & value )

这是重载函数。

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 .

void QSessionManager:: setRestartCommand (const QStringList & 命令 )

If the session manager is capable of restoring sessions it will execute 命令 in order to restore the application. The command defaults to

appname -session id
					

The -session option is mandatory; otherwise QGuiApplication cannot tell whether it has been restored or what the current session identifier is. See QGuiApplication::isSessionRestored () 和 QGuiApplication::sessionId () 了解细节。

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.

另请参阅 restartCommand (), setDiscardCommand (),和 setRestartHint ().

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

Sets the application's restart hint to hint . On application startup, the hint is set to RestartIfRunning .

注意: 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.

另请参阅 restartHint ().