本页内容

QScopedValueRollback Class

template <typename T> class QScopedValueRollback

QScopedValueRollback 类会在销毁时将变量重置为其之前的值。更多内容...

头文件: #include <QScopedValueRollback>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

详细说明

QScopedValueRollback<T> 是一个模板类,其中T 表示待管理的变量的类型。当抛出异常时,QScopedValueRollback 类可用于回滚状态,而无需编写 try-catch 代码块。

它还可用于管理临时设置的变量,例如可重入保护条件。通过使用该类,无论函数是正常退出、通过 return 语句提前退出,还是因异常而退出,该变量都会被重置。

该模板只能使用支持赋值的类型进行实例化。

另请参阅 QScopedPointer 和QScopeGuard 。

成员函数文档

[explicit constexpr] QScopedValueRollback::QScopedValueRollback(T &var)

在内部存储var 的先前值,以便在销毁时恢复。

[explicit constexpr] QScopedValueRollback::QScopedValueRollback(T &var, T value)

将value 赋值给var,并在内部保存var 的先前值,以便在销毁时恢复。

[constexpr] QScopedValueRollback::~QScopedValueRollback()

将上一个值赋给受管变量。该值是构造时或上次调用commit()时的值。

[constexpr] void QScopedValueRollback::commit()

将受管变量的先前值更新为其当前值。

© 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.