Joe Carr Joe Carr - 25 days ago 7
C Question

Changing the value of a dial slide with an input in QT?

Well, I am kind of stuck on this issue - I am trying to change the value of a dial slider and I want to change it using an input with QLineEdit input field and so far I couldn't find anything that could have helped me on the internet.

Thank you in advance!




if you do require any further information, do not hesitate to ask!



Code snip

void MainWindow::on_lineEditGain_textChanged(const QString &arg1)

{QString getVal=ui->lineEditGain->text();
// qint16 intVal = getVal.split(" ")[0].toInt();

connect(getVal, SIGNAL(textChanged(QString)), this, SLOT(setValue(QString)));}

Answer

create slots:

private slots:    
    void MySlot(QString text);

In the constructor connect textChanged() with MySlot():

connect(lineedit, SIGNAL(textChanged(QString)), this, SLOT(MySlot(QString)));

and then

void YourWidget::MySlot(QString text)
{
    QRegExp re("\\d*");  // a digit (\d), zero or more times (*)
    if (re.exactMatch(text)){
        int value = text.toInt();
        if(value >= -127 && value<=127)
          slider->setValue(value);
    }
}

You could also place a QValidator

lineedit->setValidator( new QIntValidator(-127, 127, this) );

In your case:

void MainWindow::on_lineEditGain_textChanged(const QString &arg1)
{
    QRegExp re("\\d*");  // a digit (\d), zero or more times (*)
    if (re.exactMatch(arg1)){
        int value = arg1.toInt();
        if(value >= -127 && value<=127)
            ui->slider->setValue(value);
    }
}