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. Display a Double in QlineEdit
Forum Updated to NodeBB v4.3 + New Features

Display a Double in QlineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 6 Posters 6.5k 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.
  • AbderaoufA Offline
    AbderaoufA Offline
    Abderaouf
    wrote on last edited by
    #1

    Gents;

    I have a problem displaying the result of a calculation that is a double type in a QlineEdit. The result should be less than 1, 0.75 for example, the result shows 0 though.

    Cheers;

    Raouf

    K 1 Reply Last reply
    0
    • AbderaoufA Offline
      AbderaoufA Offline
      Abderaouf
      wrote on last edited by VRonin
      #6

      Sure Ronin, please find it below:

      void CFAdvisor::SLFcalculate (QString)
      
      {
          int Crush = ui->Input_CrushResis->text().toInt();
          double AVF = ui->Input_PropAVF->text().toDouble();
          int Closure = ui->Input_Closure->text().toInt();
          int BHFP = ui->Input_BHFP->text().toInt();
          int Net = ui->Input_NetPress->text().toInt();
          double SLFresult = (Closure + Net - BHFP) / Crush;
          ui->label_SLFratio->setText(QString::number(SLFresult));
          ui->Output_SLFresult->setText(QString::number(SLFresult));
      }
      
      1 Reply Last reply
      0
      • AbderaoufA Abderaouf

        Gents;

        I have a problem displaying the result of a calculation that is a double type in a QlineEdit. The result should be less than 1, 0.75 for example, the result shows 0 though.

        Cheers;

        Raouf

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #2

        @Abderaouf

        How do you set QLineEdit with a double?

        Vote the answer(s) that helped you to solve your issue(s)

        AbderaoufA 1 Reply Last reply
        2
        • m.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by
          #3

          Hi,
          what you say looks like you are rounding the double number:

          double x=0.75;
          QString xstr=QString::number(x);
          lineEdit->setText(xstr);
          

          should do the trick.
          -Michael.

          1 Reply Last reply
          2
          • K koahnig

            @Abderaouf

            How do you set QLineEdit with a double?

            AbderaoufA Offline
            AbderaoufA Offline
            Abderaouf
            wrote on last edited by
            #4

            @koahnig
            I want to display a double result in QLineEdit or in a QLabel, i tried both but it is just showing 0. The resut is less than 1.

            VRoninV 1 Reply Last reply
            -1
            • AbderaoufA Abderaouf

              @koahnig
              I want to display a double result in QLineEdit or in a QLabel, i tried both but it is just showing 0. The resut is less than 1.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #5

              @Abderaouf Can you show us your code?

              "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
              • AbderaoufA Offline
                AbderaoufA Offline
                Abderaouf
                wrote on last edited by VRonin
                #6

                Sure Ronin, please find it below:

                void CFAdvisor::SLFcalculate (QString)
                
                {
                    int Crush = ui->Input_CrushResis->text().toInt();
                    double AVF = ui->Input_PropAVF->text().toDouble();
                    int Closure = ui->Input_Closure->text().toInt();
                    int BHFP = ui->Input_BHFP->text().toInt();
                    int Net = ui->Input_NetPress->text().toInt();
                    double SLFresult = (Closure + Net - BHFP) / Crush;
                    ui->label_SLFratio->setText(QString::number(SLFresult));
                    ui->Output_SLFresult->setText(QString::number(SLFresult));
                }
                
                1 Reply Last reply
                0
                • m.sueM Offline
                  m.sueM Offline
                  m.sue
                  wrote on last edited by
                  #7

                  You devide an integer by an integer, so you will get an integer as result. Try:
                  (double) (Closure + Net - BHFP) / Crush
                  instead.

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

                    I don't like C cast so:

                    double SLFresult = static_cast<double>(Closure + Net - BHFP) / static_cast<double>(Crush);

                    "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

                    AbderaoufA 1 Reply Last reply
                    0
                    • BuckwheatB Offline
                      BuckwheatB Offline
                      Buckwheat
                      wrote on last edited by
                      #9

                      @VRonin ... as long as you have the first cast all will be good. The integer divisor will be promoted to double. This will make your code much more readable.

                      You can use QLocale to ensure your displays are properly international and localized.

                      For example:
                      ui->label_SLFratio->setText (QLocale ().toString (SLFResult));
                      ui->Output_SLFresult->setText (QLocale ().toString (SLFResult));
                      This formats the number for the default (currently set) locale.

                      See docs: http://doc.qt.io/qt-5/qlocale.html#toString-6

                      Dave Fileccia

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

                        Hi,

                        Out of curiosity, why not use a QDoubleSpinBox to show that number ?

                        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
                        1
                        • VRoninV VRonin

                          I don't like C cast so:

                          double SLFresult = static_cast<double>(Closure + Net - BHFP) / static_cast<double>(Crush);

                          AbderaoufA Offline
                          AbderaoufA Offline
                          Abderaouf
                          wrote on last edited by
                          #11

                          @VRonin
                          Thank you Ronin. Thank was it.

                          Regards;

                          Raouf

                          1 Reply Last reply
                          0

                          • Login

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