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?
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__);
}
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)));
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With