Unsolved Pass signal to a function and emit it
-
hi, I wanted to know if it was possible to pass a signal to a function, let me explain better:
it is possible to write such function in QT C++ (Pseudocode):bool MyClass::test(int value, .... my_signal) { if( value > 0 ) { emit my_signal(value); return true; } else return false; }
And call it in this way
test(value1, MyClass::my_signal_for_value1); test(value2, MyClass::my_signal_for_value2);
Thanks in advance
-
@federico-massimi You could pass a lambda which emits the signal instead of passing signal.
-
@federico-massimi
since a signal is nothing but a function itself, you should be able to pass function pointers as arguments.Like one does in c++ :)
take a look here for some hands on stuff
https://stackoverflow.com/a/12662961 -
template <class T> bool test(int value, T my_signal) { if( value > 0 ) { (this->*my_signal)(value); return true; } return false; }
Usage
myObject.test(value1, &MyClass::my_signal_for_value1);
-
@VRonin , @federico-massimi
I like @VRonin's template-based definition above. In addition, if I understand rightly(?), his use of(this->*my_signal)(value)
means that your slot can still accessQObject::sender()
, which can be useful.