Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
QSignalBlocker Class Reference

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

#include <qobject.h>

+ Collaboration diagram for QSignalBlocker:

Public Member Functions

Q_NODISCARD_CTOR QSignalBlocker (QObject *o) noexcept
 Constructor.
 
Q_NODISCARD_CTOR QSignalBlocker (QObject &o) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Calls {object}.blockSignals(true).
 
 ~QSignalBlocker ()
 Destructor.
 
Q_NODISCARD_CTOR QSignalBlocker (QSignalBlocker &&other) noexcept
 Move-constructs a signal blocker from other.
 
QSignalBlockeroperator= (QSignalBlocker &&other) noexcept
 Move-assigns this signal blocker from other.
 
void reblock () noexcept
 Re-blocks signals after a previous unblock().
 
void unblock () noexcept
 Temporarily restores the QObject::signalsBlocked() state to what it was before this QSignalBlocker's constructor ran.
 

Detailed Description

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

Since
5.3

\inmodule QtCore

\reentrant

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
}
Exception-safe wrapper around QObject::blockSignals().
Definition qobject.h:443

is thus equivalent to

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

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

See also
QMutexLocker, QEventLoopLocker

Definition at line 442 of file qobject.h.

Constructor & Destructor Documentation

◆ QSignalBlocker() [1/3]

QSignalBlocker::QSignalBlocker ( QObject object)
inlineexplicitnoexcept

Constructor.

Calls {object}->blockSignals(true).

Definition at line 465 of file qobject.h.

◆ QSignalBlocker() [2/3]

QSignalBlocker::QSignalBlocker ( QObject o)
inlineexplicitnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Calls {object}.blockSignals(true).

Definition at line 471 of file qobject.h.

◆ ~QSignalBlocker()

QSignalBlocker::~QSignalBlocker ( )
inline

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.

Definition at line 501 of file qobject.h.

References QObject::blockSignals().

+ Here is the call graph for this function:

◆ QSignalBlocker() [3/3]

QSignalBlocker::QSignalBlocker ( QSignalBlocker &&  other)
inlinenoexcept

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.

Definition at line 477 of file qobject.h.

References other().

+ Here is the call graph for this function:

Member Function Documentation

◆ operator=()

QSignalBlocker & QSignalBlocker::operator= ( QSignalBlocker &&  other)
inlinenoexcept

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.

Definition at line 485 of file qobject.h.

References other().

+ Here is the call graph for this function:

◆ reblock()

void QSignalBlocker::reblock ( )
inlinenoexcept

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.

Definition at line 507 of file qobject.h.

References QObject::blockSignals().

+ Here is the call graph for this function:

◆ unblock()

void QSignalBlocker::unblock ( )
inlinenoexcept

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.

Definition at line 514 of file qobject.h.

References QObject::blockSignals().

Referenced by QMenuPrivate::hideMenu().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: