György Andrasek György Andrasek - 2 months ago 19
C++ Question

Qt GUI app: warning if QObject::connect() failed?

I recently migrated my Qt project from Linux to Vista, and now I'm debugging signals blindly.

On Linux, if QObject::connect() fails in a debug build, I get a warning message on stderr. On Windows, there is no console output for GUI applications, only an OutputDebugString call.

I already installed DebugView, and it catches my own qDebug() output nicely, but still no warning on failed signals.

One possible solution would be to use QtCreator's autocomplete for signals, but I like Eclipse, and using both is a PITA. Any ideas on how to get signal/slot info at runtime?

Edit: I just realized connect() returns bool, which solves the immediate problem, ugly as it may be. However, this doesn't solve the cases where QMetaObject::connectSlotsByName() fails, and this one runs automatically with widgets.

Answer

Call the static function QErrorMessage::qtHandler().

As per the documentation, this 'installs a message handler using qInstallMsgHandler() and creates a QErrorMessage that displays qDebug(), qWarning() and qFatal() messages'.

Alternatively, install a message handler with qInstallMsgHandler().

Another alternative (described in a qt-interest post) is something like this:

#ifdef _DEBUG
#define connect( connectStmt ) Q_ASSERT( connect( connectStmt ) ) 
#endif

...and for what it's worth, here are some signals and slots debugging suggestions I compiled: http://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/