Utils::Guard Class
class Utils::GuardGuard 类实现了一个带有锁定机制的递归守护机制。更多内容...
| Header: | #include <Guard> |
详细说明
它可作为QSignalBlocker 的替代方案。QSignalBlocker 会阻塞对象的所有信号,这通常并非所愿。它甚至可能阻塞对象自身内部所需的信号。而Guard和GuardLocker类则完全不会阻塞信号。
当调用某个对象的方法时,如果该方法可能会发出你已订阅的信号,而你希望忽略此通知,应将 Guard 对象作为类成员保存,并在调用该方法之前声明 GuardLocker 对象,例如:
class MyClass : public QObject { \dots private: Guard updateGuard; // member of your class }; \dots void MyClass::updateOtherObject() { GuardLocker updatelocker(updateGuard); otherObject->update(); // this may trigger a signal }
在连接到其他对象信号的槽函数内部,你可以检查 Guard 是否已被锁定,并在这种情况下忽略后续操作:
void MyClass::otherObjectUpdated() { if (updateGuard.isLocked()) return; // we didn't trigger the update // so do update now \dots }
GuardLocker 会在其析构函数中解锁 Guard。
Guard 对象是递归的,你可以为同一个 Guard 实例声明多个 GuardLocker 对象,只要为该 Guard 创建的至少一个 GuardLocker 对象仍在作用域内,Guard 就会保持锁定状态。
Copyright © The Qt Company Ltd. and other contributors. 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.