I would like to connect a QDoubleSpinBox with a horizontal QSlider. I have found enough examples to know that they require different argument types ( double vs. int).
I have found I can connect them using the new QT5 syntax:
QObject::connect(ui->gammaSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),ui->gammaHSlider, &QSlider::setValue);
QObject::connect(ui->gammaHSlider, &QSlider::sliderMoved,ui->gammaSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::setValue));
I know that to make this smooth, I need to convert their respective arguments into the others through a separate SLOT/Method. So make the QSlider int values into doubles for example:
//connect gamma hSlider to function that will change value to double
QObject::connect(ui->gammaHSlider, SIGNAL(valueChanged(int)), this, SLOT(notifyValueChanged(int)));
void MainWindow::notifyValueChanged(int value){
double doubleValue = value / 10.0;
emit doubleValueChanged(doubleValue);
ui->statusbar->showMessage(QString::number(doubleValue));
}
My Question...How do I get the QDoubleSpinBox to take this new converted value? I cant seem to understand how to get the value from the emitted signal doubleValueChanged(doubleValue)
.
Is there a correct way to do something like this?
QObject::connect(myQSlider, SIGNAL(doubleValueChanged(double)), myQDoubleSpinBox, SLOT(setValue(double)))
When I put all this together, it seems that QDoubleSpinBox will connect and sync up to the QSlider movement. But it will only display its defined value range from the UI editor.
I need my double spin box and slider to increment from 0.8 to 4, in 0.1 increments.
Solution:
---in Ui editor set the range and step of the QDoubleSpinbox and QSlider as follows:
slider:
min: 8
max: 50
step: 1
value: 10
qDoubleSpinBox:
min: 0.8
max: 5.0
step: 0.1
value: 1.0
---connect the two widgets signals to methods that update values:
QObject::connect(ui->gammaHSlider, SIGNAL(valueChanged(int)),this,SLOT(sliderValueChanged(int)));
QObject::connect(ui->gammaSpinBox, SIGNAL(valueChanged(double)),this,SLOT(spinValueChanged(double)));
---functions that update the slider and qdoublespinbox values to match scale.
void MainWindow::sliderValueChanged(int value){
double mIN = 0.8;
double mAX = 5.0;
double sTEP = 0.1;
ui->gammaHSlider->setMaximum(int (mAX/sTEP));
ui->gammaSpinBox->setDecimals(1);
ui->gammaSpinBox->setMinimum(mIN);
ui->gammaSpinBox->setMaximum(mAX);
double dVal = value*sTEP;
ui->gammaSpinBox->setValue(dVal);
ui->statusbar->showMessage(QString::number(value));
}
void MainWindow::spinValueChanged(double value){
int sVal = value*10;
ui->gammaHSlider->setValue(sVal);
ui->statusbar->showMessage(QString::number(value));
}