Connecting QSlider and QDoubleSpinBox gets stuck on certain values

264 Views Asked by At

I'm using mostly double spin boxes in my application and I'm connecting each one to a slider to sync up their values.

The method I'm using works perfectly except when the spin box value reaches 0.28, 0.56, and 1.12. When using my scroll wheel to change the slider and spin box or the spin box buttons, it gets stuck on one or all of these values and won't advance any further.

I've made a sample application with just one slider and a double spin box that exhibits the same behavior.

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->horizontalSlider, &QSlider::valueChanged, [=]() {emit changeSpinBoxValWithSliderVal(ui->horizontalSlider->value(), 0.01, "doubleSpinBox");});
    connect(ui->doubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double) {emit changeSliderValWithSpinBoxVal(ui->doubleSpinBox->value(), 100, "horizontalSlider");});
}
void MainWindow::changeSpinBoxValWithSliderVal(int sliderValue, double stepSize, QString spinBoxName)
{
    MainWindow::findChild<QDoubleSpinBox*>(spinBoxName)->setValue(double(sliderValue*stepSize));
}

void MainWindow::changeSliderValWithSpinBoxVal(double spinBoxValue, int multiplier, QString sliderName)
{
    MainWindow::findChild<QSlider*>(sliderName)->setValue(int(spinBoxValue*multiplier));
}
MainWindow::~MainWindow()
{
    delete ui;
}

QSlider range: 0-200

QSlider step size: 1

QDoubleSpinbox range: 0.00-2.00

QDoubleSpinbox step size: 0.01

spin box attributes

slider attributes

0

There are 0 best solutions below