Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

What do I do if a slot is not invoked?

A practical checklist to debug your signal/slot connections


// connect() actually returns a QMetaObject::Connection object,
// which implicitly converts to bool.
const bool connected = connect(sender, &Sender::aSignal,
                               receiver, &Receiver::aSlot);
qDebug() << "Connection established?" << connected;

// do not do this!
Q_ASSERT(connect(sender, &Sender::aSignal, receiver, &Receiver::aSlot));

const bool connected = connect(sender, &Sender::aSignal,
                               receiver, &Receiver::aSlot);
Q_ASSERT(connected);
Q_UNUSED(connected);

(gdb) b 'QObject::~QObject()' if (this == 0x12345678)

connect(sender, &Sender::aSignal, receiver, &Receiver::aSlot);
// be sure that sender and receiver didn't get destroyed:
connect(sender, &QObject::destroyed,
        [] { qDebug() << "Sender got deleted!"; });
connect(receiver, &QObject::destroyed,
        [] { qDebug() << "Receiver got deleted!"; });

// objects of this type are passed as arguments to some signal
// in a queued invocation
class MyClass {
/* ... */
}; 

// add this:
Q_DECLARE_METATYPE(MyClass);

qRegisterMetaType<MyClass>(); // do not pass any argument

// the connect to debug
connect(sender, &Sender::aSignal, receiver, &Receiver::aSlot);
// add:
connect(sender, &Sender::aSignal, // same sender and signal
        receiver,                 // context object to break this connection
        [receiver]() {            // debug output
             qDebug() << "Direct?" << QThread::currentThread() == receiver->thread();
        },
        Qt::DirectConnection);    // see below

OBJECT TextEdit::unnamed
  SIGNALS OUT
        signal: destroyed(QObject*)
        signal: destroyed()
        signal: objectNameChanged(QString)
          <functor or function pointer>
        signal: iconSizeChanged(QSize)
          --> QToolBar::unnamed _q_updateIconSize(QSize)
  SIGNALS IN
          <-- QClipboard::unnamed <unknown>
          <-- QTextDocument::unnamed <unknown>
          <-- QAction::unnamed <unknown>
          <-- QComboBox::comboSize <unknown>

GammaRay showing all the connections for a QObject

GammaRay showing signals which carry arguments which are unregistered metatypes

About KDAB


12 Comments

10 - Mar - 2017

Tim

11 - Mar - 2017

Giuseppe D'Angelo

10 - Mar - 2017

Federico

11 - Mar - 2017

Giuseppe D'Angelo

14 - Mar - 2017

Christian

17 - Mar - 2017

Giuseppe D'Angelo

3 - Jan - 2021

Doug Rogers

4 - Jan - 2021

Giuseppe D'Angelo

3 - Jan - 2021

Doug Rogers

4 - Jan - 2021

Giuseppe D'Angelo

7 - Oct - 2021

George

8 - Oct - 2021

Giuseppe D'Angelo

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Learn Modern C++

Learn more