Results 1 to 6 of 6

Thread: function pointers as parameters in signals and slots

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default function pointers as parameters in signals and slots

    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:
    Qt Code:
    1. void (*pt2Function)(void* pt2Object, QString text)
    To copy to clipboard, switch view to plain text mode 

    How do I emit it in a signal???

    ps. I have successfully declared the signal in the header file, like this:
    Qt Code:
    1. typedef void (*ApplyFunction)(void* pt2Object, QString text);
    2. signals:
    3.  
    4. void test(ApplyFunction func);
    To copy to clipboard, switch view to plain text mode 

    But when I try to emit it like this:
    Qt Code:
    1. emit test(pt2Function(pt2Object, "Name of Logged in user"));
    To copy to clipboard, switch view to plain text mode 

    I get an error. Any help appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: function pointers as parameters in signals and slots

    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:

    Qt Code:
    1. emit test(pt2Function);
    To copy to clipboard, switch view to plain text mode 

    What exactly are you trying to achieve?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: function pointers as parameters in signals and slots

    Quote Originally Posted by wysota View Post
    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:

    Qt Code:
    1. emit test(pt2Function);
    To copy to clipboard, switch view to plain text mode 

    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...lot-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???

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: function pointers as parameters in signals and slots

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: function pointers as parameters in signals and slots

    Quote Originally Posted by ggdev001 View Post
    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...lot-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.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: function pointers as parameters in signals and slots

    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.

Similar Threads

  1. Signals Slots and Class Pointers
    By Atomic_Sheep in forum Newbie
    Replies: 18
    Last Post: 7th September 2012, 09:08
  2. Signal and Slots with Large Parameters
    By metdos in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2010, 15:48
  3. Slots & Signals w/ parameters passed by reference.
    By Wazman in forum Qt Programming
    Replies: 7
    Last Post: 20th December 2008, 00:13
  4. Signals and Slots (with structure as parameters)
    By vishwanath in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 20:14
  5. Unused parameters in slots
    By ePharaoh in forum Qt Programming
    Replies: 3
    Last Post: 22nd March 2006, 08:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.