PDA

View Full Version : Slider not responding to Signal



eLancaster
25th December 2010, 16:33
I made a program that converts celsius to fahrenheit and vice versa. It consists of two sliders: sl_celsius: and sl_fheit, and two lcd displays lcd_celsius and lcd_fheit.
Moving sl_celsius displays the celsius temperature in lcd_celsius and the corresponding fahrenheit temperature in lcd_fheit. As the lcd_fheit changes, the sl_fheit also moves.
The problem is that the reverse does not occur (i.e. moving the fahrenheit slider does not cause celsius to be displayed in lcd_celsius and sl_celsius does not move either). I do not understand why this happens.

The connections are as follows:


connect(ui->sl_celsius,SIGNAL(sliderMoved(int)),ui->lcd_celsius,SLOT(display(int)));

connect(ui->sl_celsius,SIGNAL(sliderMoved(int)),this,SLOT(calc ulateFahrenheitTemperature(int)));

connect(this,SIGNAL(celsiusSliderMoved(int)),ui->lcd_fheit,SLOT(display(int)));

connect(this,SIGNAL(celsiusSliderMoved(int)),ui->sl_fheit,SLOT(setValue(int)));

connect(ui->sl_fheit,SIGNAL(sliderMoved(int)),ui->lcd_fheit,SLOT(display(int)));

connect(ui->sl_fheit,SIGNAL(sliderMoved(int)),this,SLOT(calcul ateCelsiusTemperature(int)));

connect(this,SIGNAL(fheitSliderMoved(int)),ui->lcd_celsius,SLOT(display(int)));

connect(this,SIGNAL(fheitSliderMoved(int)),ui->sl_celsius,SLOT(setValue(int)));

and these are the definitions for custom fucntions:

void TempConverter::calculateCelsiusTemperature(int temp)
{
celsius = (temp - 32)*(5/9);
emit fheitSliderMoved(celsius);
}

void TempConverter::calculateFahrenheitTemperature(int temp)
{
fahrenheit = ((temp * (9/5))+32);
emit celsiusSliderMoved(fahrenheit);
}


I'd appreciate any help, thanks!

high_flyer
25th December 2010, 17:55
One thing you should check is the output in the debugger, or in the console, if your are getting any signal/slot warnings.

At any rate, I don't know if this is the problem or not, but what you are doing is asking for trouble, since you have a circular (recursive) signal/slot connections:
if you move the celsius slider, it calls calculateFahrenheitTemperature() which trough changing the Fahrenheit slider value causes it to emit the sliderMoved() signal, which calls calculateCelsiusTemperature() which changes the cesius slider which calls calculateFahrenheitTemperature() (via the dlider signal/slot connection) again...

You need some kind of "manager" that will control both sliders, and not to have this recursion.
Once you have put such a middle object, I predict the problem will go away.
If not, ask again.

norobro
25th December 2010, 19:51
You should check the quotient of your integer division, also.

eLancaster
26th December 2010, 09:51
Yeah the problem was in the formula - I corrected it and now it works.