Na tej stronie

Utils::Guard Class

class Utils::Guard

Klasa Guard implementuje rekurencyjny mechanizm blokujący. Więcej...

Header: #include <Guard>

Szczegółowy opis

Można ją wykorzystać jako alternatywę dla klasy ` QSignalBlocker`. Klasa ` QSignalBlocker ` blokuje wszystkie sygnały obiektu, co zazwyczaj nie jest pożądane. Może ona również blokować sygnały, które są potrzebne wewnętrznie samemu obiektowi. Klasy `Guard` i `GuardLocker` w ogóle nie blokują sygnałów.

W przypadku wywołania metody obiektu, która z kolei może wyemitować sygnał, z którym jesteś połączony, a chcesz zignorować to powiadomienie, powinieneś przechowywać obiekt Guard jako element składowy swojej klasy i zadeklarować obiekt GuardLocker tuż przed wywołaniem wspomnianej metody, na przykład:

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
}

Wewnątrz slota, który jest podłączony do sygnału innego obiektu, możesz sprawdzić, czy obiekt `Guard` jest zablokowany, i w takim przypadku zignorować dalsze operacje:

void MyClass::otherObjectUpdated()
{
    if (updateGuard.isLocked())
        return;

    // we didn't trigger the update
    // so do update now
    \dots
}

GuardLocker odblokowuje Guard w swoim destruktorze.

Obiekt `Guard` jest rekurencyjny – można zadeklarować wiele obiektów `GuardLocker` dla tej samej instancji `Guard`, a obiekt `Guard` będzie zablokowany tak długo, jak długo w zakresie będzie znajdował się przynajmniej jeden obiekt `GuardLocker` utworzony dla tego obiektu `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.