ahmo ahmic
ahmo ahmic

Reputation: 19

Qt signals and slots. Slider and progress bar

connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), 
        ui->progressBar, SLOT(setValue(ui->horizontalSlider->value()-100)));

I tried to connect signals and slots when the value of slider for example is 30, the value of the progress bar should be 70 but nothing is changed, I can't find mistake.

Upvotes: 1

Views: 2185

Answers (2)

You normally do :

connect(ui->horizontalSlider, &QSlider::valueChanged, 
ui->horizontalSlider, &QSlider::setValue);

but your logic is not correct because you are connecting a valueChanged with a setValue , that will crash your app since an overflow will happen...

on the other hand, connect used to only pipe signals and slots, you can not do math in the signals/functions involved in that, at least not like that... you will need a lambda or something similiar in the middle

Upvotes: 1

Marcel Petrick
Marcel Petrick

Reputation: 484

Welcome aboard.

I am surprised that you want to do a calculation inside the connect. This is not how it works. Please add a slot (method) like

void slotSetValue(int input)
{
  ui-progressBar->setValue(100 - input);
}

and connect like connect(ui->horizontalSlider,SIGNAL(valueChanged(int)), this,slotSetValue(int)));

Fine-tuning for your code may be needed.

ps. I recommend to use the Qt5-connects, which are compile-time-checked.

Upvotes: 2

Related Questions