Many sliders with one callback

Is it possible to create several sliders and make one callback for all of them?

I create a window in which I would like to set about 10 parameters. It would be much better to have 1 callback function for all of them instead of 10 functions.

I am currently creating a trackball as follows:

cvCreateTrackbar("Var1","Window",&global_var1, 250, changing_var1); cvCreateTrackbar("Var2","Window",&global_var2, 250, changing_var2); 

and then

 void changing_var1(int pos) { global_var1 = pos; } void changing_var2(int pos) { global_var2 = pos; } 

Is it possible to create one callback that will also change all parameters according to which parameter I want to change?

+6
source share
1 answer

Yes, you should do this (at least with the C ++ interface). You will want to use the optional userData field. Below is a small example of how you can do this:

 #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace std; using namespace cv; struct ColorThresholdData { int redHigh; int redLow; }; enum ColorThresholdType { RED_HIGH, RED_LOW }; void fooCallback(int value, void* colorThreshold); struct ColorThresholdData data; int main(int argc, char** argv) { ... createTrackbar("red high", windowName, NULL, 255, fooCallback, new ColorThresholdType(RED_HIGH)); createTrackbar("red low", windowName, NULL, 255, fooCallback, new ColorThresholdType(RED_LOW)); ... } void fooCallback(int value, void* colorThreshold) { ColorThresholdType* ct = reinterpret_cast<ColorThresholdType*>(colorThreshold); switch(*ct) { case RED_HIGH: cout << "Got RED_HIGH value" << endl; data.redHigh = value; break; case RED_LOW: cout << "Got RED_LOW value" << endl; data.redLow = value; break; } } 

Hope this is what you were looking for :)

+3
source

All Articles