Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect slider and line edit??
Forum Updated to NodeBB v4.3 + New Features

Connect slider and line edit??

Scheduled Pinned Locked Moved Unsolved General and Desktop
connectqsliderqlineeditshared value
11 Posts 3 Posters 8.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • QT-static-prgmQ Offline
    QT-static-prgmQ Offline
    QT-static-prgm
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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));});
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • QT-static-prgmQ Offline
          QT-static-prgmQ Offline
          QT-static-prgm
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            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

            http://doc.qt.io/qt-5/qtglobal.html#qOverload

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • QT-static-prgmQ Offline
              QT-static-prgmQ Offline
              QT-static-prgm
              wrote on last edited by QT-static-prgm
              #6

              @VRonin both do not work

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

              1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • QT-static-prgmQ Offline
                  QT-static-prgmQ Offline
                  QT-static-prgm
                  wrote on last edited by
                  #8

                  doublespinbox. I found the problem. see my edit above

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • QT-static-prgmQ Offline
                      QT-static-prgmQ Offline
                      QT-static-prgm
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved