May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

Function pointers in Qt

Page  
1

Hello guys, :)

I’m trying to load a function pointer to a class of mine. But I’m getting a “non-matching function or call” when compiling.

my classes constructor looks like this:

  1. LinkedSliderDoubleSpinBox::LinkedSliderDoubleSpinBox(double minValue, double maxValue, long int bins, void (*setValueFunction)(double), double (*getValueFunction)(), QWidget *parent) :
  2.     QWidget(parent)
  3. {...}

my call looks like this:

  1. LinkedSliderDoubleSpinBox* constantFieldDirChanger = new LinkedSliderDoubleSpinBox(minConstantDir,maxConstantDir,sliderLength,((MainWindow*)parentWidget())->openGLApp->blochsim->setConstantFieldDirection,((MainWindow*)parentWidget())->openGLApp->blochsim->getConstantFieldDirection,this);

min and maxConstantDir are doubles, sliderLength is an integer
and the functions setConstantFieldDirection and getConstantFieldDirection look have the following prototypes:

  1.     void setConstantFieldDirection(double value);
  2.     double getConstantFieldDirection();

The class I created in order to link a QSlider to a QDoubleSpinBox.

I’m not so familiar with function pointers. So I don’t really know whether I did a good mistake with them. Could you guys please help? why doesn’t the call match the prototype?

Thank you. Any efforts are highly apprecaited! :-)

29 replies

May 12, 2011

yshurik yshurik
Lab Rat
40 posts

You are trying to pass function – member of class.
You have to use function adaptors in such case like std::mem_fun

May 12, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

Hi,

first moved this to C++, as the problem is a generic C++ problem, not a Qt problem.

Function pointers MUST use C – functions, not member functions of classes. A function pointer might never be a C++ member function pointer. To achieve such things, you would need functors, like you get from boost.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

Thank you for the answers, guys!

Interesting! I thought I could simply use the pointer of any function!

I’ve put this here because I thought it has to do with Qt class hierarchy.

Could you guys please post an example of how I could use function adaptors to handle this situation? I don’t really get the idea whether I have to use it in the constructors parameters or call or so on.

May 12, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

The problem is, that normal “C” functions only need their parameters. A class member also needs the this pointer. These functor ideas (also function adapters) bind both together.

It’s sometimes not the trivialst thing, but you should google for it.
Like here [stackoverflow.com]

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 12, 2011

yshurik yshurik
Lab Rat
40 posts

Kind of:

  1. class A{
  2.  void setX(int value);
  3. };
  4.  
  5. template<typename Set, typename Value>
  6. void change_obj_by_func(A * obj, Set set, Value value) {
  7.   set(obj, value);
  8. }
  9.  
  10. ...
  11. A * a = new A;
  12. change_obj_by_func(a, std::mem_fun(&A::x), 35);

Benefit of this to have possibility of applying different member functions in same code and this member function can be passed as parameter.

May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

The problem is that I’m not allowed to edit Blochsim. So I need to use function adaptors. I tried the following (as it’s done in the link you gave me, Gerolf) but it doesn’t compile

  1.     constantFieldDirChanger = new LinkedSliderDoubleSpinBox(minConstantDir,maxConstantDir,sliderLength,std::bind1st(mem_fun(&(((MainWindow*)parentWidget())->openGLApp->blochsim->setConstantFieldDirection)),this),std::bind1st(mem_fun(&(((MainWindow*)parentWidget())->openGLApp->blochsim->getConstantFieldDirection)),this),this);

Error is: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&Blochsim::setConstantFieldDirection’

Any ideas?

May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

Thank you for your example. Unfortunately, your example doesn’t imitate my problem. The function I want to call is supposed to edit the object’s member variables of the class… not only use it within the members of the current stack!

Let’s rephrase the problem. Is there anyway (not necessarily function pointers) to pass blochsim object’s member function (that affect the object’s member variables of blochsim) to my class to use it later when needed?

May 12, 2011

yshurik yshurik
Lab Rat
40 posts
TheDestroyer wrote:
Let’s rephrase the problem. Is there anyway (not necessarily function pointers) to pass blochsim object’s member function (that affect the object’s member variables of blochsim) to my class to use it later when needed?

Why not? Just keep the functor + pointer to object to invoke them later

May 12, 2011

HuXiKa HuXiKa
Lab Rat
83 posts

You could also try the following method to call a class member:

  1. #define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))
  2. typedef < return_type > (MyClass::*pt2Member)(< parameters >);
  3. ...
  4. // somewhere where you want to call a member function
  5. pt2Member p = &Myclass::memberfunction;
  6. CALL_MEMBER_FN(< pointer_to_MyClass_object >, p)(< parameters >)

But this is still based on the fact Gerolf said: you need a this parameter to call a member function.

 Signature 

If you can find faults of spelling in the text above, you can keep them.

May 12, 2011

Andre Andre
Area 51 Engineer
6076 posts

This is basic C++ stuff. Perhaps the C++ FAQ [parashift.com] would be a good guide here.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

But I’m not allowed to edit Blochsim that has the function that is going to edit the variable I need! I can’t “create” a functor for Blochsim! the thing I’m doing is passing a set and get functions. Can I convert these functions to functors?

Could you please elaborate more on that if I misunderstood you?

May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

HuXiKa, thank you for your efforts. But I don’t want to call a member function. I want to pass it to a function.

May 12, 2011

SteveKing SteveKing
Lab Rat
105 posts

Not sure if I’m missing something, but if you want to link a QSlider to a QDoubleSpinBox why not just use signals and slots?

May 12, 2011

TheDestroyer TheDestroyer
Lab Rat
145 posts

I want to have them automatically linked in this class, because I have like 20 instances of this.

May 12, 2011

SteveKing SteveKing
Lab Rat
105 posts

In that case create a QSlider and a QDoubleSpinBox and link them in the constructor of the LinkedSliderDoubleSpinBox. You can also do all the laying out of the slider and the spin box within the LinkedSliderDoubleSpinBox at this point. You could always derive from QFrame rather than QWidget to allow borders etc.

When you want to use the LiLinkedSliderDoubleSpinBox just create one. If you use layouts etc. it should give you a nice widget you can use anywhere.

Steve

Page  
1

  ‹‹ [Solved] Problem defining a std::endl manipulator for a custom class      displaying image using label in Qt4.7 ››

You must log in to post a reply. Not a member yet? Register here!