Is there a way to use any C++ function as a Qt slot, without having its class inheriting from QWidget?
-
I am not sure if it is a duplicated, but this might help : http://stackoverflow.com/questions/8533906/ – BЈовић Mar 29 '12 at 09:12
5 Answers
You cannot in Qt versions < Qt 5.
In order to use signals/slots the meta object compiler has to be invoked. To make this happen your class should meet the following requirements:
- Inherit from
QObject
or any other subclass (egQWidget
,QPushButton
etc) - The
Q_OBJECT
macro should be defined in the private section of the class in order to enable meta-object features such as slots - Use the
Qt
keywordsslots
andsignals
in order to declare which functions should be handles by the meta compiler as slots or signals
For more details check the corresponding documentation pages about the meta-object system and the signals & slots
Also check the QObject
documentation:
Notice that the
Q_OBJECT
macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.
Edit: Since Qt 5, functors and lambda expressions can be used as slot. See New Signal Slot Syntax in Qt 5
-
1Importantly, however, while this is not possible in Qt 4, it IS going to be possible in Qt 5, using the new signal/slot mechanism. http://qt-project.org/wiki/New_Signal_Slot_Syntax – leinir Mar 29 '12 at 10:56
-
You are right, but IMHO it should be used only in rare cases. Using slots is more elegant than using tr1::bind with simple functions. – pnezis Mar 29 '12 at 11:04
-
i absolutely agree, they should be a rare occurance. A lot like short cirquit if statements, they are useful in a very narrow set of circumstances, and in those cases increase readability and ease of maintenance. But, they should only be used then, not as a general rule. – leinir Jul 18 '12 at 17:23
Since Qt 5, functors and lambda expressions can be used as slot (as previously mentioned, here: http://qt-project.org/wiki/New_Signal_Slot_Syntax).
As I could not find example code, I added the following:
This example uses boost::function
for a class member ClassName::classMember()
without parameters.
boost::function<void()> f= boost::bind(&ClassName::classMember, classInstance);
connect(QObjectInstance, &QObject::signalName, f);
When the Qt signal and class member have parameters (for instance ClassName::classMember(int)
), the boost function should be adapted as follows:
boost::function<void(int)> f= boost::bind(&ClassName::classMember, classInstance, _1);
More information on boost::bind can be found in the documentation: http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html

- 1,746
- 1
- 20
- 35
In Qt 5, you can. See http://qt-project.org/wiki/New_Signal_Slot_Syntax
In Qt 4, this is not directly supported but there are a couple of libraries which provide this functionality by proxying the signal via a slot in a hidden QObject which then calls your non-slot function. My attempt at this can be found at https://github.com/robertknight/qt-signal-tools . There are links to other implementations on the bottom of the README.

- 2,888
- 27
- 21
Unlikely. The Qt-meta-object-compiler (moc) wraps the function, are marked as slot-s, in relatively large code-wrapper. Files, created by moc, begin with moc_, look them.

- 88
- 4
It is enough to inherit QObject: http://qt-project.org/doc/qt-4.8/signalsandslots.html

- 7,971
- 3
- 31
- 41