本页内容

QSessionManager Class

QSessionManager 类提供了对会话管理器的访问。更多内容...

头文件: #include <QSessionManager>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
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 &command)
void setManagerProperty(const QString &name, const QStringList &value)
void setManagerProperty(const QString &name, const QString &value)
void setRestartCommand(const QStringList &command)
void setRestartHint(QSessionManager::RestartHint hint)

详细说明

在桌面环境(Qt GUI 应用程序运行于此)中,会话管理器负责跟踪会话——即一组正在运行的应用程序,每个应用程序都处于特定的状态。应用程序的状态主要包括(最显著的是)该应用程序已打开的文档,以及其窗口的位置和大小。

会话管理器用于保存会话(例如,当机器关机时),并恢复会话(例如,当机器启动时)。 我们建议您使用 `QSettings ` 来保存应用程序的设置,例如窗口位置、最近使用的文件等。当应用程序由会话管理器重新启动时,您可以恢复这些设置。

QSessionManager 提供了应用程序与平台会话管理器之间的接口。在 Qt 中,会话管理操作请求由两个信号QGuiApplication::commitDataRequest() 和QGuiApplication::saveStateRequest() 处理。这两个信号都以 QSessionManager 对象的引用作为参数。只有在由这些信号调用的槽中,才能访问会话管理器。

除非应用程序从会话管理器处获得明确许可,否则无法进行任何用户交互。您可以通过调用allowsInteraction() 来请求许可;如果情况非常紧急,也可以调用allowsErrorInteraction()。Qt 不会强制执行此规则,但会话管理器可能会强制执行。

您可以尝试通过调用cancel() 来中止关机过程。

对于 Unix/X11 上提供的复杂会话管理器,QSessionManager 提供了更多用于精细调整应用程序会话管理行为的选项:setRestartCommand()、setDiscardCommand()、setRestartHint()、setProperty()、requestPhase2()。更多详细信息请参阅相应函数的说明。

另请参阅 QGuiApplication 和会话管理。

成员类型文档

enum QSessionManager::RestartHint

此枚举类型定义了在何种情况下,该应用程序希望由会话管理器将其重启。当前的取值包括:

常量值描述
QSessionManager::RestartIfRunning0如果会话关闭时应用程序仍在运行,则希望在下一个会话开始时重新启动该应用程序。
QSessionManager::RestartAnyway1无论发生什么情况,应用程序都希望在下一会话开始时启动。(这对于那些仅在系统启动后运行、随后即退出的小工具非常有用。)
QSessionManager::RestartImmediately2只要应用程序未运行,就希望立即启动它。
QSessionManager::RestartNever3应用程序不希望被自动重启。

默认提示为RestartIfRunning 。

成员函数文档

bool QSessionManager::allowsErrorInteraction()

如果允许错误交互,则返回true ;否则返回false 。

这与allowsInteraction() 类似,但还允许应用程序向用户报告发生的任何错误。会话管理器可能会赋予错误交互请求更高的优先级,这意味着错误交互更有可能被允许。但是,仍不能保证会话管理器一定会允许交互。

另请参阅 allowsInteraction()、release() 和cancel()。

bool QSessionManager::allowsInteraction()

向会话管理器请求与用户交互的权限。如果允许交互,则返回 true;否则返回false 。

该机制的设计初衷是确保在关机过程中能够同步用户的交互操作。高级会话管理器可能会同时要求所有应用程序提交数据,从而显著加快关机速度。

当交互完成后,我们强烈建议通过调用release()来释放用户交互信号量。这样,当您的应用程序仍在忙于保存数据时,其他应用程序就有机会与用户进行交互。(当应用程序退出时,该信号量会隐式释放。)

如果用户在交互阶段决定取消关机过程,您必须通过调用cancel() 来通知会话管理器已发生此情况。

以下是一个应用程序的QGuiApplication::commitDataRequest() 可能的实现示例:

MyMainWidget::MyMainWidget(QWidget*parent)
    : QWidget(parent)
{
    connect(qApp, &QGuiApplication::commitDataRequest,
           this, &MyMainWidget::commitData);
}

voidMyMainWidget::commitData(QSessionManager&manager)
{
    if(manager.allowsInteraction()) {
        intret=QMessageBox::warning(
                    mainWindow,
                    tr("My Application"),
                    tr("将更改保存到文档吗?"),
                    QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);

        switch(ret) {
        caseQMessageBox::Save:
            manager.release();
            if(!saveDocument())
                manager.cancel();
            break;
        caseQMessageBox::Discard:
            break;
        caseQMessageBox::取消:
        默认:
            manager.cancel();
        }
    }else{
        // 若未获得交互权限,则
        // 执行其他合理操作
   }
}

如果应用程序在保存数据时发生错误,建议改用allowsErrorInteraction() 方法。

另请参阅 QGuiApplication::commitDataRequest()、release() 和cancel()。

void QSessionManager::cancel()

指示会话管理器取消关机过程。应用程序在未事先征得用户同意的情况下,不应调用此函数。

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

QStringList QSessionManager::discardCommand() const

返回当前设置的丢弃命令。

另请参阅 setDiscardCommand()、restartCommand() 和setRestartCommand()。

bool QSessionManager::isPhase2() const

如果会话管理器当前正在执行第二个会话管理阶段,则返回true ;否则返回false 。

另请参阅 requestPhase2()。

void QSessionManager::release()

在交互阶段结束后释放会话管理器的交互信号量。

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

void QSessionManager::requestPhase2()

请求为该应用程序启动第二个会话管理阶段。随后,该应用程序可立即从QGuiApplication::commitDataRequest()或QApplication::saveStateRequest()函数中返回,待大多数或所有其他应用程序完成其会话管理后,这些函数将被再次调用。

这两个阶段对于 X11 窗口管理器等应用程序非常有用,此类应用程序需要存储其他应用程序窗口的相关信息,因此必须等待这些应用程序完成各自的会话管理任务。

注意:如果 另一个应用程序已请求进入第二阶段,则该应用程序的第二阶段可能会在您的应用程序的第二阶段之前、同时或之后被调用。

另请参阅 isPhase2()。

QStringList QSessionManager::restartCommand() const

返回当前设置的重启命令。

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

QSessionManager::RestartHint QSessionManager::restartHint() const

返回应用程序的当前重启提示。默认值为RestartIfRunning 。

另请参阅 setRestartHint()。

QString QSessionManager::sessionId() const

返回当前会话的标识符。

如果应用程序是从较早的会话中恢复的,则该标识符与较早会话中的标识符相同。

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

QString QSessionManager::sessionKey() const

返回当前会话中的会话密钥。

如果应用程序是从较早的会话中恢复的,则该密钥与上一个会话结束时的密钥相同。

每次调用 commitData() 或 saveState() 时,会话密钥都会发生变化。

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

void QSessionManager::setDiscardCommand(const QStringList &command)

将“discard”命令设置为指定的command 。

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

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

对应用程序标识和状态记录的低级写入访问权限保存在会话管理器中。

名为name 的属性,其值被设置为字符串列表value 。

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

对应用程序标识和状态记录的低级写入访问权限保存在会话管理器中。

名为name 的属性其值被设置为字符串value 。

这是一个重载函数。

void QSessionManager::setRestartCommand(const QStringList &command)

如果会话管理器支持恢复会话,它将执行command 命令来恢复应用程序。该命令的默认选项为

appname -session id

-session 选项是必填的;否则,QGuiApplication 无法判断应用程序是否已被恢复,也无法得知当前的会话标识符。详情请参阅QGuiApplication::isSessionRestored() 和QGuiApplication::sessionId()。

如果您的应用程序非常简单,或许可以将整个应用程序状态存储在额外的命令行选项中。但这通常是个非常糟糕的主意,因为命令行长度通常限制在几百字节以内。相反,请使用 `QSettings`、临时文件或数据库来实现此目的。 通过使用唯一的sessionId()标记数据,您将在未来的会话中能够恢复应用程序。

另请参阅 restartCommand()、setDiscardCommand() 和setRestartHint()。

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

将应用程序的重启提示设置为hint 。应用程序启动时,该提示将设置为RestartIfRunning 。

注意:这些 标志仅为提示,会话管理器可能遵循也可能不遵循这些提示。

我们建议在 `QGuiApplication::saveStateRequest()` 中设置重启提示,因为大多数会话管理器会在应用程序启动后不久执行检查点操作。

另请参阅 restartHint()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.