Results 1 to 8 of 8

Thread: How to pass a member funtion to another object using funtion pointer

  1. #1
    Join Date
    Nov 2009
    Posts
    60
    Thanks
    3
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default How to pass a member funtion to another object using funtion pointer

    Hi All,

    I want to pass a member funtion of my QWidget class to constructor of my QDialog subclass.

    example :

    class popup : public QDialog,private Ui:opup
    {
    Q_OBJECT

    public:
    popup(QWidget *parent,void (Widget::*)(QString &));
    ~popup();

    };

    I get the following error on compilation

    error: no matching function for call to ‘popup:opup(Widget* const, <unresolved overloaded function type>)’


    Kindly help in fixing these issue.

    Ratheendran

  2. #2
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to pass a member funtion to another object using funtion pointer

    Your error is caused by calling the constructor, so you need to provide the line where you call the constructor of popup, probably along with the definition of "Widget". Otherwise we cannot help. Btw. your function-pointer has no name...

    Btw. this question would have been more appropriate in a C++ forum..

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to pass a member funtion to another object using funtion pointer

    And if you tell what you want to do, someone might give an alternative solution.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to pass a member funtion to another object using funtion pointer

    In C++, this is not simple, due to encapsulation and inheritance. The syntax becomes rather messy.

    For more information, see here.

    Many times, a functor can be used in places where a function pointer would otherwise appear. Once in a while, though, only a function pointer will do.

  5. #5
    Join Date
    Nov 2009
    Posts
    60
    Thanks
    3
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to pass a member funtion to another object using funtion pointer

    Thanks for your suggestions.

    this is where I call the constructor of popup and I get the above error message.

    void Widget:n_pushButton_clicked()
    {
    popup popDia(this,putString);
    popDia.exec();
    }

    Just trying to implement a call back in c++.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to pass a member funtion to another object using funtion pointer

    What is the definition of 'Widget' ? You seem to be referring to it as a base case, but you don't seem to inherit from it?

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to pass a member funtion to another object using funtion pointer

    I would recommend using functors, also known as function objects, in place of the cumbersome function pointer syntax. They are perfect for use as callbacks.

    Of course, signals and slots do away with much of the need for callback functions; you can simply connect the objects that need to communicate.

  8. #8
    Join Date
    Feb 2006
    Posts
    32
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to pass a member funtion to another object using funtion pointer

    You still haven't provided the definition of Widget, so its hard to help. From the error message I would guess, that you have two putString functions in Widget, and hence the compiler is unable to resolve which one to take.
    As an alternative approach, apart from a functor (function object) you could also use the listener concept (i.e. observer pattern).

Similar Threads

  1. Replies: 11
    Last Post: 14th March 2010, 22:00
  2. Replies: 14
    Last Post: 1st December 2009, 20:45
  3. Replies: 2
    Last Post: 7th July 2009, 17:44
  4. ActiveQt: How to pass image to ActiveX object?
    By AndreasSchlempp in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2009, 12:44
  5. static Object Vs Pointer
    By rajeshs in forum General Programming
    Replies: 4
    Last Post: 11th June 2008, 07:41

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.