×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    passing function pointer as QT slots

    Hi All,

    Is it possible to pass function pointer with QT slots?

    lets say...

    void postRequest( void (*pf)(QNetworkReply*))
    {
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(pf(QNetworkReply*)));
    }

    Its giving the following error for me:
    Object::connect: No such slot pf(QNetworkReply*)

    Any suggestions passing function pointers to slots, would be appreciated.

    Regards
    Rajini

  2. #2
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: passing function pointer as QT slots

    A slot parameter is a string representing slot signature. You cannot pass function pointer there.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    Re: passing function pointer as QT slots

    Any suggestions, how could i pass different slot functions for the same signal in QT ?

  4. #4
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: passing function pointer as QT slots

    Note that name of framework is Qt. I don't know what could be use-case for such problem, but you can pass string representing slot's signature instead of pointer.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    Re: passing function pointer as QT slots

    Thanks a lot!!

    I would go for other solutions..

  6. #6
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: passing function pointer as QT slots

    Quote Originally Posted by g.rkreddy View Post
    Thanks a lot!!

    I would go for other solutions..
    I suggest you to explain use-case more detailed.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    Re: passing function pointer as QT slots

    My application sends requests to facebook to get the required information, these requests and responses are asynchronous in nature. Each response is handled different slot, so to map the request and reply slot, i should pass both of them to the same function parameter like .... void postRequest( QString &request_method, void (*pf)(QNetworkReply*)). so when i get the response i will be calling appropriate slot for the request. This is my usecase

  8. #8
    Nokia Developer Moderator
    Join Date
    Oct 2009
    Posts
    4,326

    Re: passing function pointer as QT slots

    But we already discussed that you can identify calls by QNetworkReply pointer in the same slot? You can get pointer with QObject::sender()
    http://doc.qt.nokia.com/4.7/qobject.html#sender
    Alternatively you can check QSignalMapper
    http://doc.qt.nokia.com/4.7/qsignalmapper.html

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    28

    Re: passing function pointer as QT slots

    I got a simple solution for the problem

    void postRequest(QString &request_method, const char* slot) {
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, slot);
    }

    postRequest("getfriends",SLOT(friendsRequestFinished(QNetworkReply*)));
    postRequest("getfriends",SLOT(messageRequestFinished(QNetworkReply*)));


    Daniil, thanks a lot!!

  10. #10
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: passing function pointer as QT slots

    Note that when it comes to passing function pointers in C++ (and Qt is C++), the "preferred" technique is to create multiple subclasses of a root class, each implementing a different function for, say, "doit()", and pass instances of the subclasses. This is typesafe and reasonably efficient.

Similar Threads

  1. Passing parameter from symbian c++ to a javascript function
    By phpgenerator in forum Symbian Web Runtime
    Replies: 9
    Last Post: 2010-06-21, 13:09
  2. Replies: 2
    Last Post: 2008-03-26, 15:59
  3. Passing RPointerArray as Function argument
    By sohilr in forum Symbian C++
    Replies: 3
    Last Post: 2007-10-08, 13:23
  4. Passing & returning parameter to/from function
    By jarruots in forum Symbian C++
    Replies: 3
    Last Post: 2006-11-21, 12:17
  5. passing gfx contect to a function
    By mj32 in forum Symbian C++
    Replies: 2
    Last Post: 2003-12-22, 11:05

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •