Function pointers in Qt
-
wrote on 12 May 2011, 11:58 last edited by
Kind of:
@
class A{
void setX(int value);
};template<typename Set, typename Value>
void change_obj_by_func(A * obj, Set set, Value value) {
set(obj, value);
}...
A * a = new A;
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.
-
wrote on 12 May 2011, 11:59 last edited by
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
@
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?
-
wrote on 12 May 2011, 12:20 last edited by
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?
-
wrote on 12 May 2011, 12:22 last edited by
[quote author="TheDestroyer" date="1305202824"]
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?[/quote]Why not? Just keep the functor + pointer to object to invoke them later
-
wrote on 12 May 2011, 12:29 last edited by
You could also try the following method to call a class member:
@#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
typedef < return_type > (MyClass::*pt2Member)(< parameters >);
...
// somewhere where you want to call a member function
pt2Member p = &Myclass::memberfunction;
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.
-
wrote on 12 May 2011, 12:29 last edited by
This is basic C++ stuff. Perhaps the "C++ FAQ":http://www.parashift.com/c++-faq-lite/pointers-to-members.html would be a good guide here.
-
wrote on 12 May 2011, 12:30 last edited by
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?
-
wrote on 12 May 2011, 12:44 last edited by
HuXiKa, thank you for your efforts. But I don't want to call a member function. I want to pass it to a function.
-
wrote on 12 May 2011, 14:01 last edited by
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?
-
wrote on 12 May 2011, 14:27 last edited by
I want to have them automatically linked in this class, because I have like 20 instances of this.
-
wrote on 12 May 2011, 15:07 last edited by
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
-
wrote on 12 May 2011, 15:13 last edited by
The problem is not creating and linking them. The problem is linking them together to the "set" function that is going to do the changes in the simulation. The set function is located in another class (blochsim). I don't have variables access in that class, just set and get functions to access the variables.
-
wrote on 12 May 2011, 15:16 last edited by
If you always connect the same types, it is absolutly no problem:
@
LinkedSliderDoubleSpinBox::LinkedSliderDoubleSpinBox(double minValue, double maxValue, long int bins, QSlider* pSlider, QWidget *parent) :
QWidget(parent)
{
connect(pSlider, SIGNAL(valueChanged(int), this, SLOT(setValue(int));
connect(this, SIGNAL(valueChanged(int), pSlider, SLOT(setValue(int));
setValue(pSlider->value());
}
@ -
wrote on 12 May 2011, 17:15 last edited by
Here is the thing. setValue(...) is a member function of another class's object, where I want to call the function INSIDE that object after to set a value INSIDE that object.
Since this operation is not specific and isn't supposed to control a single situation, or in other words one object's variable but rather apply the same slider with the spinbox to many other variables (this is the idea in the first place), I want to pass the "setValue(...)" function to the class, so that the class could use it as it needs.
Doesn't this look difficult?
-
wrote on 12 May 2011, 18:17 last edited by
If you want to work with any type of objects, and those objects are QObject derived classes AND the value to set is a property, you could also use Qt's meta object system so set and get the property. :-) Is that elegant enough?
-
wrote on 12 May 2011, 18:43 last edited by
ahhhhh, man! this is veryyyyyyyyyyyyyyyy diasppointing. The class isn't Qt derived :D
but I think I'm starting to get the solution. but I need your help!
I'm reading a book called "Professional C++" by Solter and Kleper.
An example solution to my problem is the following (under Pointers to Methods):
@
SpreadsheetCell myCell;
double (SpreadsheetCell::*methodPtr) () const = &SpreadsheetCell::getValue;
cout << myCell.*methodPtr) () << endl;
@I'm trying to do like this code as follows:
@
double (Blochsim::*getConstantFieldDirPtr) () const = &Blochsim::getConstantFieldDirection();
@
but this line is giving me the following error:cannot call member function 'double Blochsim::getConstantFieldDirection()' without an object.
I tried to define and object from the class Blochsim (which I know doesn't make sense because it's in another line):
@
Blochsim bs;
double (Blochsim::*getConstantFieldDirPtr) () const = &Blochsim::getConstantFieldDirection();
@
but still gives the same error!any idea how to solve this??? :D I'm happy I'm getting close to the solution!!!
-
wrote on 12 May 2011, 19:20 last edited by
remove the brakets :-)
@
double (Blochsim::*getConstantFieldDirPtr) () const = &Blochsim::getConstantFieldDirection;
@But where do you get the object instance from to call the method on?
-
wrote on 12 May 2011, 19:36 last edited by
It would be:
@
(bs.*getConstantFieldDirPtr)();
@Example:
@
class TObj {
public:
double funcA() {
return 0;
}};
int main(int argc, char *argv[])
{
TObj tobj;double (TObj::*funcAPtr)() = &TObj::funcA; (tobj.*funcAPtr)();
}
@I've write some years an similar code on PalmOS.
-
wrote on 12 May 2011, 19:44 last edited by
But then again, you need the object where you call it. If I understood correctly, he does not want to have the object in present. otherwise, he would not need that.
I would go with "boost::bind":http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#with_boost_function
-
wrote on 12 May 2011, 19:56 last edited by
I agree.
15/30