이 페이지에서

Utils::Guard Class

class Utils::Guard

Guard 클래스는 잠금 메커니즘이 포함된 재귀적 가드 기능을 구현합니다. 더 보기...

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
}

다른 객체의 시그널에 연결된 슬롯 내부에서는 가드가 잠겨 있는지 확인하고, 이 경우 후속 작업을 무시할 수 있습니다:

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.