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. Get Value of DoubleSpinBox into a double or float
QtWS25 Last Chance

Get Value of DoubleSpinBox into a double or float

Scheduled Pinned Locked Moved Solved General and Desktop
doubledoublespinboxget values of
7 Posts 4 Posters 2.8k Views
  • 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.
  • T Offline
    T Offline
    tomaschku
    wrote on 30 Oct 2017, 19:56 last edited by tomaschku
    #1

    Hello again,
    I have a simple calculation:

    double a = QVariant(ui->input_a->text()).toDouble();
    int b = QVariant(ui->input_b->text()).toInt();
    double d = 0;
    
    d = b / (a * a);
    
    ui->text_result->setText(QVariant(d).toString());
    

    When I compile it, no error is shown, but when I edit the Values with the DoubleSpinBoxes, then the Value a doesn't change. (i.e. It's 0.)

    Code for checking Values:

    ui->text_result->setText("Error! " + QVariant(a).toString() + "|" + QVariant(b).toString());
    

    Before, I used Floats, but because I read, it was better, when I use double I changed the format. (Because of precision, ...)
    The Debugger isn't saying anything about this.

    V 1 Reply Last reply 31 Oct 2017, 20:41
    0
    • J Offline
      J Offline
      Joel Bodenmann
      wrote on 30 Oct 2017, 23:09 last edited by Joel Bodenmann
      #2

      QVariant doesn't not do the conversion for you. It's just a means to store any type, but if you store a QString in it, then QVariant::toDouble() will not convert it to a double for you.

      Instead, use QString::toDouble():

      bool ok = false;
      double a = ui->input_a->text().toDouble(&ok);
      if (!ok) {
          qDebug("Conversion failed!");
      }
      

      Reasons for why QString::toDouble() might fail are simple format issues. You can prevent those by using a QValidator. Alternatively, if your input_a contains composite strings you can use a regex to extract your double.

      @tomaschku said in Get Value of DoubleSpinBox into a double or float:

      Before, I used Floats, but because I read, it was better, when I use double I changed the format. (Because of precision, ...)

      The question is whether you need the precision.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      K 1 Reply Last reply 31 Oct 2017, 20:36
      1
      • J Offline
        J Offline
        Joel Bodenmann
        wrote on 30 Oct 2017, 23:14 last edited by
        #3

        Err... I just read your title... You seem to use QDoubleSpinBox? If so, why not just using QDoubleSpinBox::value() which returns a double? There is no QDoubleSpinBox::text() so I'm not sure if you're mixing things up (or if you're using QDoubleSpinBox::textFromValue() but in any case, if you're using QDoubleSpinBox then using QDoubleSpinBox::value() is the right way to go. If you use something else, such as QLineEdit, then the stuff I mentioned in my first post is the way to go.

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        T 1 Reply Last reply 31 Oct 2017, 15:17
        4
        • J Joel Bodenmann
          30 Oct 2017, 23:14

          Err... I just read your title... You seem to use QDoubleSpinBox? If so, why not just using QDoubleSpinBox::value() which returns a double? There is no QDoubleSpinBox::text() so I'm not sure if you're mixing things up (or if you're using QDoubleSpinBox::textFromValue() but in any case, if you're using QDoubleSpinBox then using QDoubleSpinBox::value() is the right way to go. If you use something else, such as QLineEdit, then the stuff I mentioned in my first post is the way to go.

          T Offline
          T Offline
          tomaschku
          wrote on 31 Oct 2017, 15:17 last edited by
          #4

          @Joel-Bodenmann Thank you, for your answer. I'm very new with the UI-Version of C++ and I don't know so much about it... :)

          J 1 Reply Last reply 31 Oct 2017, 15:19
          0
          • T tomaschku
            31 Oct 2017, 15:17

            @Joel-Bodenmann Thank you, for your answer. I'm very new with the UI-Version of C++ and I don't know so much about it... :)

            J Offline
            J Offline
            Joel Bodenmann
            wrote on 31 Oct 2017, 15:19 last edited by
            #5

            You're welcome - we're here to help :)

            Please mark this topic as solved if your questions have been answered.

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            0
            • J Joel Bodenmann
              30 Oct 2017, 23:09

              QVariant doesn't not do the conversion for you. It's just a means to store any type, but if you store a QString in it, then QVariant::toDouble() will not convert it to a double for you.

              Instead, use QString::toDouble():

              bool ok = false;
              double a = ui->input_a->text().toDouble(&ok);
              if (!ok) {
                  qDebug("Conversion failed!");
              }
              

              Reasons for why QString::toDouble() might fail are simple format issues. You can prevent those by using a QValidator. Alternatively, if your input_a contains composite strings you can use a regex to extract your double.

              @tomaschku said in Get Value of DoubleSpinBox into a double or float:

              Before, I used Floats, but because I read, it was better, when I use double I changed the format. (Because of precision, ...)

              The question is whether you need the precision.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 31 Oct 2017, 20:36 last edited by
              #6

              @Joel-Bodenmann said in Get Value of DoubleSpinBox into a double or float:

              QVariant doesn't not do the conversion for you.

              Just as a note here, QVariant actually does conversions. Whether or not a conversion is possible can be judged by the return value of QVariant::canConvert.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • T tomaschku
                30 Oct 2017, 19:56

                Hello again,
                I have a simple calculation:

                double a = QVariant(ui->input_a->text()).toDouble();
                int b = QVariant(ui->input_b->text()).toInt();
                double d = 0;
                
                d = b / (a * a);
                
                ui->text_result->setText(QVariant(d).toString());
                

                When I compile it, no error is shown, but when I edit the Values with the DoubleSpinBoxes, then the Value a doesn't change. (i.e. It's 0.)

                Code for checking Values:

                ui->text_result->setText("Error! " + QVariant(a).toString() + "|" + QVariant(b).toString());
                

                Before, I used Floats, but because I read, it was better, when I use double I changed the format. (Because of precision, ...)
                The Debugger isn't saying anything about this.

                V Offline
                V Offline
                VRonin
                wrote on 31 Oct 2017, 20:41 last edited by
                #7

                @tomaschku said in Get Value of DoubleSpinBox into a double or float:

                Code for checking Values:
                ui->text_result->setText("Error! " + QVariant(a).toString() + "|" + QVariant(b).toString());

                when I edit the Values with the DoubleSpinBoxes, then the Value a doesn't change. (i.e. It's 0.)

                I think the problem is even worse than what is mentioned above.
                double a = ui->input_a->value(); does not imply that when input_a changes it automatically updates a

                "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

                1/7

                30 Oct 2017, 19:56

                • Login

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