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オブジェクトが少なくとも1つスコープ内にある限り、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.