1 | initial version |
the trackbar callback expects a global, static callback function (it has to save the function address). if you want to use this in a class context, you need some indirection ("dispatch" from a static handler to your desired class member function):
struct Foo {
int value;
// static function
static void callback(int pos, void *userptr) {
Foo *foo = (Foo*)usrptr; // cast user data back to "this"
foo->real_callback(pos);
}
// class member function
void real_callback(int p) {
// do something.
// you can use Foo's "this" ptr here.
this->value = p;
}
};
...
Foo foo;
// now pass Foo's *static* callback, and it's address:
cv::createTrackbar(win_name, bar_name, foo.value, 100, Foo::callback, (void*)(&foo));
2 | No.2 Revision |
the trackbar callback expects a global, static callback function (it has to save the function address). if you want to use this in a class context, you need some indirection ("dispatch" from a static handler to your desired class member function):
struct Foo {
int value;
// static function
static void callback(int pos, void *userptr) {
Foo *foo = (Foo*)usrptr; // cast user data back to "this"
foo->real_callback(pos);
}
// class member function
void real_callback(int p) {
// do something.
// you can use Foo's "this" ptr here.
this->value = p;
}
};
...
Foo foo;
// now pass Foo's *static* callback, and it's address:
cv::createTrackbar(win_name, bar_name, foo.value, &foo.value, 100, Foo::callback, (void*)(&foo));