Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
György Andrasek Avatar asked Sep 28 '09 11:09

György Andrasek


2 Answers

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/

like image 185
Sam Dutton Avatar answered Oct 10 '22 12:10

Sam Dutton


The solution I like for this is to set

QT_FATAL_WARNINGS=1

in the environment of the program when you debug. That makes the program crash, giving you a nice backtrace, especially if you run the code in a debugger. If you don't want the crash, see the answer above.

like image 39
Esben Mose Hansen Avatar answered Oct 10 '22 12:10

Esben Mose Hansen