PDA

View Full Version : function pointers as parameters in signals and slots



ggdev001
21st February 2013, 14:50
Hello, I am trying to use function pointers as parameters in signals and slots
but get some compiler errors. What would be the right syntax to do it???
Thanks.

ps. This is my function pointer for example:

void (*pt2Function)(void* pt2Object, QString text)

How do I emit it in a signal???

ps. I have successfully declared the signal in the header file, like this:


typedef void (*ApplyFunction)(void* pt2Object, QString text);
signals:

void test(ApplyFunction func);


But when I try to emit it like this:

emit test(pt2Function(pt2Object, "Name of Logged in user"));


I get an error. Any help appreciated.

wysota
21st February 2013, 14:59
Hmmm.... This certainly won't work. A pointer to a function is exactly that -- a pointer to a function. What you are trying to "emit" is a signal "test" with a single argument which is a result of a function call (that returns void so the argument to test is also void). If you wanted to pass a function pointer, the statement would have to look like this:


emit test(pt2Function);

What exactly are you trying to achieve?

ggdev001
21st February 2013, 15:08
Hmmm.... This certainly won't work. A pointer to a function is exactly that -- a pointer to a function. What you are trying to "emit" is a signal "test" with a single argument which is a result of a function call (that returns void so the argument to test is also void). If you wanted to pass a function pointer, the statement would have to look like this:


emit test(pt2Function);

What exactly are you trying to achieve?

it's a bit hard to explain fully what I want now, but basically I wanted to know a correct syntax how to pass function pointers as parameters in signals and slots.
The example here mentions in generally it is not OK however they also mention a workaround: http://qt-project.org/doc/qt-4.8/moc.html#function-pointers-cannot-be-signal-or-slot-parameters
So, I basically wanted to know what is the correct syntax to emit a signal with function pointer as a parameter -- and then also to catch it in the slot???

wysota
21st February 2013, 15:32
But why would you want to emit a function pointer in an argument in the first place?

BTW. You can always pass a functor if you really want to. But then why not pass an object implementing some interface instead?

amleto
21st February 2013, 19:04
it's a bit hard to explain fully what I want now, but basically I wanted to know a correct syntax how to pass function pointers as parameters in signals and slots.
The example here mentions in generally it is not OK however they also mention a workaround: http://qt-project.org/doc/qt-4.8/moc.html#function-pointers-cannot-be-signal-or-slot-parameters
So, I basically wanted to know what is the correct syntax to emit a signal with function pointer as a parameter -- and then also to catch it in the slot???

your question is answered in your own link. Although why you'd even want to do that is beyond me.

d_stranz
21st February 2013, 23:19
Although why you'd even want to do that is beyond me.

Maybe there is some confusion about signals and slots vs. callbacks.

Wysota's solution is the most suitable - wrap the function pointer and the arguments to the function in an object, and pass a reference to that object as the argument to the signal. In the slot, retrieve the function pointer and arguments, then call the function.

But this doesn't make sense unless there is only a single slot handling the signal, otherwise the function would be invoked for every connected slot. Seems that a callback mechanism would be more suitable in that case.

If the slot added its own arguments to the function, then each call would in fact be different. If not, then why bother with the signals and slots at all? At the time the signal is emitted, you already know all there is to know about invoking the function, so just simply invoke it there instead of sending a signal for some other object to perform the identical action.