Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I be alerted when Qt signal/slot connection fails?

We lose a lot of time when using a connect from/to a non-existing signal/slot, because Qt only warns us at runtime somewhere in the console logging.

Apart from evolving to Qt5, which uses the type system to report these problems, and from changing code for all connect calls in the system, is there another way to have the Qt runtime e.g. throw, or simply crash, or alert me loudly, when a wrong connection is made?

like image 599
xtofl Avatar asked Apr 25 '14 07:04

xtofl


2 Answers

You can use a wrapper on connect which halts the program when some connection fails:

inline void CHECKED_CONNECT( const QObject * sender, const char * signal,
             const QObject * receiver,  const char * method,
             Qt::ConnectionType type = Qt::AutoConnection )
{
  if(!QObject::connect(sender, signal, receiver, method, type))
   qt_assert_x(Q_FUNC_INFO, "CHECKED_CONNECT failed", __FILE__, __LINE__);
}
like image 190
Nejat Avatar answered Sep 28 '22 19:09

Nejat


My compact variant is as follows:

// BoolVerifier.h
#include <cassert>    

class BoolVerifier
{
public:
    BoolVerifier() = default;
    inline BoolVerifier(bool b) { assert(b); (void)(b); }
    inline BoolVerifier& operator=(bool b) { assert(b); (void)(b); return *this; }
};

And usage:

BoolVerifier b;
b = connect(objectFrom, SIGNAL(mySignal1(int)), objectTo, SLOT(mySlot1(int)));
b = connect(objectFrom, SIGNAL(mySignal2(int)), objectTo, SLOT(mySlot2(int)));
...
like image 36
antonio Avatar answered Sep 28 '22 17:09

antonio