QSignalBlocker Class

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

Header: #include <QSignalBlocker>
qmake: QT += core
Since: Qt 5.3

Note: All functions in this class are reentrant.

Public Functions

QSignalBlocker(QObject *o)
QSignalBlocker(QObject &o)
QSignalBlocker(QSignalBlocker &&other)
~QSignalBlocker()
void reblock()
void unblock()
QSignalBlocker &operator=(QSignalBlocker &&other)

Detailed Description

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

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

QSignalBlocker::QSignalBlocker(QObject *o)

Default constructs an instance of QSignalBlocker.

QSignalBlocker::QSignalBlocker(QObject &o)

Default constructs an instance of QSignalBlocker.

QSignalBlocker::QSignalBlocker(QSignalBlocker &&other)

Move-copy constructor.

QSignalBlocker::~QSignalBlocker()

Destroys the instance of QSignalBlocker.

void QSignalBlocker::reblock()

void QSignalBlocker::unblock()

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

Move-assignment operator.