Nominate our 2022 Qt Champions!

Connect slider and line edit??

  • hey,

    is there a fast way to connect a slider with a line edit??

    at the moment i'm doing it that way:

    #include "..\Header\SettingsWindow.h"
    #include <QString>
    #include <QLineEdit>
    #include <QSlider>
    SettingsWindow::SettingsWindow(QWidget * parent)
    	: QWidget(parent)
    	, ui(new Ui::SettingsWindow)
    	setWindowFlags(Qt::Tool | Qt::NoDropShadowWindowHint);
    	connect(ui->lightOff_R_LE, SIGNAL(textChanged(QString)), this, SLOT(lightOffRValueChanged(QString)));
    	connect(ui->lightOff_R_S, SIGNAL(valueChanged(int)), this, SLOT(lightOffRValueChanged(int)));
    	delete ui;
    void SettingsWindow::lightOffRValueChanged(QString value)
    void SettingsWindow::lightOffRValueChanged(int value)
    void SettingsWindow::lightOffGValueChanged(QString value)
    void SettingsWindow::lightOffGValueChanged(int value)
    void SettingsWindow::lightOffBValueChanged(QString value)
    void SettingsWindow::lightOffBValueChanged(int value)

    But there are 9 of these combinations, so i thought there is maybe a faster and easier way to do it

  • Lifetime Qt Champion


    You can use lambdas. Or you could also use QSpinBox rather than QLineEdit and thus you could connect the two directly.

  • If you can use Qt5 and C++11

    connect(ui->lightOff_R_LE, &QLineEdit::textChanged, [this](const QString& val)->void{ui->lightOff_R_S->setValue(val.toInt());});
    	connect(ui->lightOff_R_S, &QSlider::valueChanged,[this](const int& val)->void{ui->lightOff_R_LE->setText(locale().toString(val));});

  • @VRonin do you know how do bypass this error message?

    connect(ui->ambCoef, &QSpinBox::valueChanged, [this](const double& value) {emit updateAmbCoef(value); });

    it says it is not clear what instance of the overloaded function valueChanged should be used

  • connect(ui->ambCoef, qOverload<double>(&QDoubleSpinBox::valueChanged), [this](const double& value) {emit updateAmbCoef(value); });

    or if you are using qt<5.7

    connect(ui->ambCoef, ststic_cast<void (&QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), [this](const double& value) {emit updateAmbCoef(value); });


  • @VRonin both do not work

    just see you had a typo.
    static_cast<void (QDoubleSpinBox::*)(double)>
    instead of
    static_cast<void (&QDoubleSpinBox::*)(double)>

  • what is ui->ambCoef ? it's unclear here if it's a QDoubleSpinBox or a QSpinBox

  • doublespinbox. I found the problem. see my edit above

  • Lifetime Qt Champion

    Out of curiosity, why use a lambda in that case and not signal forwarding ?

  • Because it does not work. I get qstring and need into and the other way around.

  • Lifetime Qt Champion

    But why use QLineEdit if you are only manipulating numbers ? That makes the code uselessly complicated.