Can I pass signals and slots as arguments?

  • Here's what I would like to do. I am writing a plugin architecture where the plugins are able to generate QWidgets. Those QWidgets will have signals and slots based on some consistent naming convention so that the signals and slots can be matched up appropriately. For instance, maybe one of the widgets has a signal called highlightThingSignal and another widget has a slot called highlightThingSlot. What I need to do to make this work is call a method in the plugin that returns the widget, a list of its signals and a list of its slots. And then after matching them appropriately I need to be able to connect them. Is there a way to 'pass' signals and slots as arguments or maybe another better way to accomplish the same thing.


  • First of all, you can find out which signals and slots any object has by using QMetaObject. Qt 4.8 will introduce a connect() overload taking QMetaMethods.

    You can also just return a list of char* (or QByteArrays) containing their names, and do the connect yourself. Just remember to prepend a literal '1' or '2' to your signals/slots when you pass them to connect() (see the definition of the SIGNAL / SLOT macros).

  • in Qt you can see some function which uses signal/slot as argument. Check: QMenu::addAction

    You can pass the macro SIGNAL/SLOT as arguments this will make the necessary adjustments to get the correct signature.

    something like that: menu.addAction("text", recever, SLOT(myslot()));

  • At first glance at least it looks like QMetaObject along with your suggestion about the prepended "1" or "2" will work just fine! The beauty of the solution is that the plugin developer doesn't even need to pass the list of signals and slots. They just create them and it works. Being able to pass QMetaMethods to connect will be nice.


  • i my own program, i need to do a similar thing, but i instead pass the name of the slot function as the parameter in the form of QString.

    and i get the method's id with QMetaObject()->methodId("function name");

    and call the function with QMetaMethod()->invoke(...)

Log in to reply