Connect slider and line edit??
-
wrote on 23 Jan 2017, 14:21 last edited by
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) { ui->setupUi(this); 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))); } SettingsWindow::~SettingsWindow() { delete ui; } void SettingsWindow::lightOffRValueChanged(QString value) { ui->lightOff_R_S->setValue(value.toInt()); } void SettingsWindow::lightOffRValueChanged(int value) { ui->lightOff_R_LE->setText(QString::number(value)); } void SettingsWindow::lightOffGValueChanged(QString value) { ui->lightOff_G_S->setValue(value.toInt()); } void SettingsWindow::lightOffGValueChanged(int value) { ui->lightOff_G_LE->setText(QString::number(value)); } void SettingsWindow::lightOffBValueChanged(QString value) { ui->lightOff_B_S->setValue(value.toInt()); } void SettingsWindow::lightOffBValueChanged(int value) { ui->lightOff_B_LE->setText(QString::number(value)); }
But there are 9 of these combinations, so i thought there is maybe a faster and easier way to do it
-
Hi,
You can use lambdas. Or you could also use QSpinBox rather than QLineEdit and thus you could connect the two directly.
-
wrote on 23 Jan 2017, 15:26 last edited by
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));});
-
wrote on 24 Jan 2017, 15:12 last edited by
@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
-
wrote on 24 Jan 2017, 15:16 last edited by VRonin
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); });
References:
https://wiki.qt.io/New_Signal_Slot_Syntax -
wrote on 24 Jan 2017, 15:37 last edited by QT-static-prgm
@VRonin both do not work
just see you had a typo.
static_cast<void (QDoubleSpinBox::*)(double)>
instead of
static_cast<void (&QDoubleSpinBox::*)(double)>
-
wrote on 24 Jan 2017, 15:40 last edited by
what is
ui->ambCoef
? it's unclear here if it's a QDoubleSpinBox or a QSpinBox -
wrote on 24 Jan 2017, 15:41 last edited by
doublespinbox. I found the problem. see my edit above
-
Out of curiosity, why use a lambda in that case and not signal forwarding ?
-
wrote on 24 Jan 2017, 22:45 last edited by
Because it does not work. I get qstring and need into and the other way around.
-
But why use QLineEdit if you are only manipulating numbers ? That makes the code uselessly complicated.
1/11