PDA

View Full Version : linking a slider to a global variable



switch
29th July 2009, 13:31
Hi guys,

I have successfully connected a slider and a spinBox so that if i change the value in one the other will update, however i dont want to connect the spinBox to the slider, i actually link the slider to a global variable so that when the slider changes so will the value in the variable.

My code to connect a spinBox and a Slider is below:

QObject::connect(spinBox_saturation, SIGNAL(valueChanged(int)), horzSlider_saturation, SLOT(setValue(int)));

And the variable i want to connect to this slider is

int output;

wagmare
29th July 2009, 13:49
your class as getSliderValue()


private slots:
void valueReceived(int);

connect(horzSlider_saturation, SIGNAL(valueChanged(int)), getSliderValue, SLOT(valueReceived(int));


void getSliderValue::valueReceived(int output)
{
//output will have the value now
}

switch
31st July 2009, 19:32
Hi,
Thanks for your solution but i am having problems implimenting it. I don't understand why i need to create a seperate class to handle passing the variable.

Is it not possible to just pass the variable as it stands, i have tried this idea myself and whereas the code compiles correctly the values do not seem to be passing from the slider to the variable.

My idea:

QObject::connect(horzSlider_hueMax, SIGNAL(sliderMoved(int)), this, SLOT(setValue(hMax)));

wagmare
1st August 2009, 07:56
code will compile but in runtime it will show in the console

Object::connect: No such slot Test::setValue(hMax)
if u try to connect a SIGNAL .. it should be a SLOT and not a variable or function ..

SLOT may be static like close(), quit(), etc..
also user defined like setValue(int)

and another thing ... u cannot connect a SIGNAL(int) to SLOT(hMax)

a SIGNAL(int) can be connect to SLOT(int) or SLOT(void)
but not to SLOT(QString&) or SLOT(hMAx)

switch
4th August 2009, 16:14
Hi wagmare, thanks for explaining your solution it works fine.

However i came up with another method in the mean time.

I realised that the spinBox has a function called value() which allows you to access the current value contained in the box, so using this function and by linking the slider to a spin box i managed to connect the global variable to my slider.

int hMax;
hMax = spinBox_hueMax->value();


I am sure you can use the same technique to connect a slider directly to the variable.

int hMax;
hMax = slider_hueMax->value();

Ginsengelf
5th August 2009, 06:52
Hi, this way your variable will not be updated if the slider changes. If you need this, use wagmare's example and connect the slider to a slot.

Ginsengelf

wagmare
5th August 2009, 08:02
thanks ginsengelf ..
look

int hMax;
hMax = slider_hueMax->value();

hMax will get the value only once when you call the above line .. if u want to test it every second u can use QTimer to check the value every second of the QSlider

but connect() will return the value whenver the user change or Qt change the QSlider value and that value is readily available in hMax .

every time the QSlider is called update() .. the connect respond with the QSlider signal emission which we can get it from SLOT()