QSignalBlocker Class

Exception-safe wrapper around QObject::blockSignals(). More...

Header: #include <QSignalBlocker>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Note: All functions in this class are reentrant.

Public Functions

QSignalBlocker(QObject *object)
QSignalBlocker(QObject &object)
QSignalBlocker(QSignalBlocker &&other)
~QSignalBlocker()
(since 6.7) void dismiss()
void reblock()
void unblock()
QSignalBlocker &operator=(QSignalBlocker &&other)

Detailed Description

QSignalBlocker can be used wherever you would otherwise use a pair of calls to blockSignals(). It blocks signals in its constructor and in the destructor it resets the state to what it was before the constructor ran.

{
const QSignalBlocker blocker(someQObject);
// no signals here
}

is thus equivalent to

const bool wasBlocked = someQObject->blockSignals(true);
// no signals here
someQObject->blockSignals(wasBlocked);

except the code using QSignalBlocker is safe in the face of exceptions.

See also QMutexLocker and QEventLoopLocker.

Member Function Documentation

[explicit noexcept] QSignalBlocker::QSignalBlocker(QObject *object)

Constructor. Calls object->blockSignals(true).

[explicit noexcept] QSignalBlocker::QSignalBlocker(QObject &object)

This is an overloaded function.

Calls object.blockSignals(true).

[noexcept] QSignalBlocker::QSignalBlocker(QSignalBlocker &&other)

Move-constructs a signal blocker from other. other will have a no-op destructor, while responsibility for restoring the QObject::signalsBlocked() state is transferred to the new object.

[noexcept] QSignalBlocker::~QSignalBlocker()

Destructor. Restores the QObject::signalsBlocked() state to what it was before the constructor ran, unless unblock() has been called without a following reblock(), in which case it does nothing.

[noexcept, since 6.7] void QSignalBlocker::dismiss()

Dismisses the QSignalBlocker. It will no longer access the QObject passed to its constructor. unblock(), reblock(), as well as ~QSignalBlocker() will have no effect.

This function was introduced in Qt 6.7.

[noexcept] void QSignalBlocker::reblock()

Re-blocks signals after a previous unblock().

The numbers of reblock() and unblock() calls are not counted, so every reblock() undoes any number of unblock() calls.

[noexcept] void QSignalBlocker::unblock()

Temporarily restores the QObject::signalsBlocked() state to what it was before this QSignalBlocker's constructor ran. To undo, use reblock().

The numbers of reblock() and unblock() calls are not counted, so every unblock() undoes any number of reblock() calls.

[noexcept] QSignalBlocker &QSignalBlocker::operator=(QSignalBlocker &&other)

Move-assigns this signal blocker from other. other will have a no-op destructor, while responsibility for restoring the QObject::signalsBlocked() state is transferred to this object.

The object's signals this signal blocker was blocking prior to being moved to, if any, are unblocked except in the case where both instances block the same object's signals and *this is unblocked while other is not, at the time of the move.

© 2024 The Qt Company Ltd. 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.