Utils::Guard Class
class Utils::GuardLa classe Guard implementa un guard ricorsivo con meccanismo di blocco. Per saperne di più...
| Header: | #include <Guard> |
Descrizione dettagliata
Può essere utilizzata come alternativa a ` QSignalBlocker`. ` QSignalBlocker ` blocca tutti i segnali dell'oggetto, cosa che solitamente non è auspicabile. Potrebbe inoltre bloccare segnali necessari internamente all'oggetto stesso. Le classi `Guard` e `GuardLocker` non bloccano affatto i segnali.
Quando si chiama un metodo di un oggetto che a sua volta potrebbe emettere un segnale a cui si è collegati e si desidera ignorare questa notifica, è necessario mantenere l'oggetto Guard come membro della propria classe e dichiarare l'oggetto GuardLocker appena prima di chiamare il metodo in questione, come segue:
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 }
All’interno di uno slot collegato al segnale di un altro oggetto, è possibile verificare se il Guard è bloccato e, in tal caso, ignorare le operazioni successive:
void MyClass::otherObjectUpdated() { if (updateGuard.isLocked()) return; // we didn't trigger the update // so do update now \dots }
Il GuardLocker sblocca il Guard nel proprio distruttore.
L’oggetto `Guard` è ricorsivo: è possibile dichiarare più oggetti `GuardLocker` per la stessa istanza di `Guard` e il `Guard` rimarrà bloccato fintanto che almeno un oggetto `GuardLocker` creato per quel `Guard` si trovi nell’ambito di validità.
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.