KGlobal::checkedConnect() (review request)

David Faure faure at kde.org
Mon Apr 26 12:13:30 BST 2010


Too many times, we don't notice the warning from Qt about
a failed signal/slot connection, and we wonder much later why
things don't work.
Especially while refactoring keditbookmarks, I was thinking, it would be nice
to get an instant assert when a signal/slot connection fails, rather than having
to monitor stderr after every change.

QObject::connect returns a bool so we can react on this, but of course
Q_ASSERT(QObject::connect(...)) would be very wrong, it would compile
it out in release mode.
This led me to write the following method:

Index: kglobal.cpp
===================================================================
--- kglobal.cpp (revision 1118942)
+++ kglobal.cpp (working copy)
@@ -316,3 +316,10 @@
     return 0;
 
 }
+
+void KGlobal::checkedConnect(QObject* sender, const char* sig, QObject* receiver, const char* slot)
+{
+    const bool connectOk = QObject::connect(sender, sig, receiver, slot);
+    Q_ASSERT(connectOk);
+    Q_UNUSED(connectOk); // fix warning in release mode
+}
Index: kglobal.h
===================================================================
--- kglobal.h   (revision 1118942)
+++ kglobal.h   (working copy)
@@ -490,6 +490,15 @@
     }
 
     /**
+     * A version of QObject::connect() which asserts (with Q_ASSERT) in case the connection failed.
+     * This is useful so that wrong signatures for signals and slots can be detected at connect()
+     * time rather than when debugging the application and things don't work.
+     *
+     * @since 4.5
+     */
+    void checkedConnect(QObject* sender, const char* sig, QObject* receiver, const char* slot);
+
+    /**
      * For setLocale
      */
     enum CopyCatalogs { DoCopyCatalogs, DontCopyCatalogs};


OK for commit?

-- 
David Faure, faure at kde.org, http://www.davidfaure.fr
Sponsored by Nokia to work on KDE, incl. Konqueror (http://www.konqueror.org).




More information about the kde-core-devel mailing list