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. converting from float or double to QString results in output being 0
QtWS25 Last Chance

converting from float or double to QString results in output being 0

Scheduled Pinned Locked Moved Solved General and Desktop
c++qstringconversionline editfloat
23 Posts 4 Posters 3.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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #2

    @Dean21 said in converting from float or double to QString results in output being 0:

    amplification_multiplier

    what is the type of this identifier?

    1 Reply Last reply
    0
    • D Dean21

      as it says in title I have a float or double variable (tried both) and I want to convert them into type QString to be used in lineEdit, however all the methods I have seen online to do this result in my output being 0;

      Shown below is the relevant code;

      #include "widget.h"
      #include "./ui_widget.h"
      #include <QPalette>
      #include <QDebug>
      #include <QTimer>
      
      
      #include <string>
      #include <cstring>
      #include <iostream>
      #include <time.h>
      #include </home/dave/mosquitto/include/mosquitto.h> //needed for mosquitto MQTT
      
      //-----for qt qrapper for mosquitto-----
      #include <QCoreApplication>
      #include <QTime>
      #include "QMMqttClient.h"
      //--------------------------------------
      
      #define amplification_multiplier 10;
      
      QString msg_as_qstring;
      
      
      //------------IP Adresses------------
      char RPi_IP[] = "130.246.58.64";
      //-----------------------------------
      
      
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::Widget)
      
      {
          ui->setupUi(this);
      
      
              QObject::connect(&client, &QMMqttClient::onConnected, this ,[this](){
                  qDebug() << " QMMqttClient::onConnected handler, subscribe to topic...";
                  client.subscribeTopic("Light_Level");
              });
      
              QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                  msg_as_qstring = QString(msg);
                  qDebug() << "test value is"<< msg_as_qstring;
                  msg_functionality();
                  Light_level_before_amplification(msg_as_qstring);
              });
      
              qDebug() << "msg_as_qstring"<< msg_as_qstring;
      
      //        /* An example of unsecure connection */
              client.initialize("12", RPi_IP, 1883);
      
              client.connect();
      
      
      }
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      
      void Widget::msg_functionality(){
          ui->Light_Level_Line_Edit->setText(msg_as_qstring);
      }
      
      
      void Widget::Light_level_before_amplification(QString msg_as_Qstring){
         
          qDebug() << "Qstring as float " << msg_as_Qstring;
      
          double before_amplification = msg_as_Qstring.toDouble()/amplification_multiplier;
      
          //QString msg_as_qstring_after_conv = QString::number(before_amplification);
      
          //msg_as_qstring_after_conv.setNum(before_amplification);
          
          //QString msg_as_qstring_after_conv = QString(before_amplification);
      
          qDebug() << "msg as qstring conv" << msg_as_qstring_after_conv;
      
          ui->Light_Level_before_amp_Line_Edit->setText(msg_as_qstring_after_conv);
      
      }
      

      In the function light_level_before_amplification the commented out lines are the methods I have tried and couldnt get to work, the only line that produces errors is QString msg_as_qstring_after_conv = QString(before_amplification); but the other 2 methods dont give errors but just result in an output of 0.

      QString msg_as_Qstring can be any decimal value

      here is the output when I run one of the methods that doesnt introduce errors (both give same output)

      Topic:  "Light_Level" , Msg:  "5.73628\u0000"
      test value is "5.73628\u0000"
      Qstring as float  "5.73628\u0000"
      msg as qstring conv "0"
      

      Any help would be great,
      Thanks in advance,
      Dean

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @Dean21
      Please show (e.g. as qDebug() on the numbers, not strings) msg_as_Qstring.toDouble(), amplification_multiplier and before_amplification .

      Kent-DorfmanK D 2 Replies Last reply
      0
      • JonBJ JonB

        @Dean21
        Please show (e.g. as qDebug() on the numbers, not strings) msg_as_Qstring.toDouble(), amplification_multiplier and before_amplification .

        Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #4

        @JonB pretty sure OP is dividing a float by an int. D'Oh!

        1 Reply Last reply
        0
        • D Dean21

          as it says in title I have a float or double variable (tried both) and I want to convert them into type QString to be used in lineEdit, however all the methods I have seen online to do this result in my output being 0;

          Shown below is the relevant code;

          #include "widget.h"
          #include "./ui_widget.h"
          #include <QPalette>
          #include <QDebug>
          #include <QTimer>
          
          
          #include <string>
          #include <cstring>
          #include <iostream>
          #include <time.h>
          #include </home/dave/mosquitto/include/mosquitto.h> //needed for mosquitto MQTT
          
          //-----for qt qrapper for mosquitto-----
          #include <QCoreApplication>
          #include <QTime>
          #include "QMMqttClient.h"
          //--------------------------------------
          
          #define amplification_multiplier 10;
          
          QString msg_as_qstring;
          
          
          //------------IP Adresses------------
          char RPi_IP[] = "130.246.58.64";
          //-----------------------------------
          
          
          Widget::Widget(QWidget *parent)
              : QWidget(parent)
              , ui(new Ui::Widget)
          
          {
              ui->setupUi(this);
          
          
                  QObject::connect(&client, &QMMqttClient::onConnected, this ,[this](){
                      qDebug() << " QMMqttClient::onConnected handler, subscribe to topic...";
                      client.subscribeTopic("Light_Level");
                  });
          
                  QObject::connect(&client, &QMMqttClient::onMessageReceived, [this](const QString &topic, const QByteArray &msg) {
                      msg_as_qstring = QString(msg);
                      qDebug() << "test value is"<< msg_as_qstring;
                      msg_functionality();
                      Light_level_before_amplification(msg_as_qstring);
                  });
          
                  qDebug() << "msg_as_qstring"<< msg_as_qstring;
          
          //        /* An example of unsecure connection */
                  client.initialize("12", RPi_IP, 1883);
          
                  client.connect();
          
          
          }
          
          Widget::~Widget()
          {
              delete ui;
          }
          
          
          void Widget::msg_functionality(){
              ui->Light_Level_Line_Edit->setText(msg_as_qstring);
          }
          
          
          void Widget::Light_level_before_amplification(QString msg_as_Qstring){
             
              qDebug() << "Qstring as float " << msg_as_Qstring;
          
              double before_amplification = msg_as_Qstring.toDouble()/amplification_multiplier;
          
              //QString msg_as_qstring_after_conv = QString::number(before_amplification);
          
              //msg_as_qstring_after_conv.setNum(before_amplification);
              
              //QString msg_as_qstring_after_conv = QString(before_amplification);
          
              qDebug() << "msg as qstring conv" << msg_as_qstring_after_conv;
          
              ui->Light_Level_before_amp_Line_Edit->setText(msg_as_qstring_after_conv);
          
          }
          

          In the function light_level_before_amplification the commented out lines are the methods I have tried and couldnt get to work, the only line that produces errors is QString msg_as_qstring_after_conv = QString(before_amplification); but the other 2 methods dont give errors but just result in an output of 0.

          QString msg_as_Qstring can be any decimal value

          here is the output when I run one of the methods that doesnt introduce errors (both give same output)

          Topic:  "Light_Level" , Msg:  "5.73628\u0000"
          test value is "5.73628\u0000"
          Qstring as float  "5.73628\u0000"
          msg as qstring conv "0"
          

          Any help would be great,
          Thanks in advance,
          Dean

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Dean21 said in converting from float or double to QString results in output being 0:

          5.73628\u0000

          As you can see your string contains also \u0000. First remove the trailing zero byte (\u0000) from the string then do the conversion.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • JonBJ JonB

            @Dean21
            Please show (e.g. as qDebug() on the numbers, not strings) msg_as_Qstring.toDouble(), amplification_multiplier and before_amplification .

            D Offline
            D Offline
            Dean21
            wrote on last edited by
            #6

            @JonB Hi and Hi @Kent-Dorfman the amplification_multiplier is #defined at the top of the code I posted

            output for the debugs you mentioned

            Topic:  "Light_Level" , Msg:  "5.73628\u0000"
            test value is "5.73628\u0000"
            Qstring as float  "5.73628\u0000"
            before amp 0
            amp multiplier 10
            msg as qstring to double 0
            msg as qstring conv "0"
            

            And hi @jsulm how would I go about being able to do this?

            Thanks, Dean

            JonBJ jsulmJ 2 Replies Last reply
            0
            • D Dean21

              @JonB Hi and Hi @Kent-Dorfman the amplification_multiplier is #defined at the top of the code I posted

              output for the debugs you mentioned

              Topic:  "Light_Level" , Msg:  "5.73628\u0000"
              test value is "5.73628\u0000"
              Qstring as float  "5.73628\u0000"
              before amp 0
              amp multiplier 10
              msg as qstring to double 0
              msg as qstring conv "0"
              

              And hi @jsulm how would I go about being able to do this?

              Thanks, Dean

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @Dean21 said in converting from float or double to QString results in output being 0:

              msg as qstring to double 0

              So that is the only thing you need to look at.

              And hi @jsulm how would I go about being able to do this?

              Look in QString docs to see how you can remove the last character?

              1 Reply Last reply
              1
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #8

                so what is (float)5.7 / (int)10?

                Therein is the reason you are getting zero!

                D JonBJ 2 Replies Last reply
                0
                • D Dean21

                  @JonB Hi and Hi @Kent-Dorfman the amplification_multiplier is #defined at the top of the code I posted

                  output for the debugs you mentioned

                  Topic:  "Light_Level" , Msg:  "5.73628\u0000"
                  test value is "5.73628\u0000"
                  Qstring as float  "5.73628\u0000"
                  before amp 0
                  amp multiplier 10
                  msg as qstring to double 0
                  msg as qstring conv "0"
                  

                  And hi @jsulm how would I go about being able to do this?

                  Thanks, Dean

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Dean21 Something like msg_as_Qstring.remove(QChar("\u0000"));

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  D 1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #10

                    I question whether removing non-numeric char at end of QSTring is necessary for the numeric conversion problem since all the conversion code I'm familiar with scans digits until it finds an char that can't be part of the number, and then returns everything up to that point.

                    Not saying it doesn't need to be addressed, but I don't see it as the reason the problem exists.

                    1 Reply Last reply
                    0
                    • Kent-DorfmanK Kent-Dorfman

                      so what is (float)5.7 / (int)10?

                      Therein is the reason you are getting zero!

                      D Offline
                      D Offline
                      Dean21
                      wrote on last edited by
                      #11

                      @Kent-Dorfman I did try #define amplfication_multiplier 10.0 instead of #define amplification_multiplier 10, but it made no difference to the final output

                      1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Dean21 Something like msg_as_Qstring.remove(QChar("\u0000"));

                        D Offline
                        D Offline
                        Dean21
                        wrote on last edited by
                        #12

                        @jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part

                        msg_as_Qstring.remove(("[\u0000]."));
                        

                        but it still has it attached on the end here is the output

                        Topic:  "Light_Level" , Msg:  "5.73628\u0000"
                        test value is "5.73628\u0000"
                        Qstring as float  "5.73628\u0000"
                        before amp 0
                        amp multiplier 10
                        msg as qstring to double 0
                        msg as qstring conv "0"
                        
                        JonBJ jsulmJ 2 Replies Last reply
                        0
                        • D Dean21

                          @jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part

                          msg_as_Qstring.remove(("[\u0000]."));
                          

                          but it still has it attached on the end here is the output

                          Topic:  "Light_Level" , Msg:  "5.73628\u0000"
                          test value is "5.73628\u0000"
                          Qstring as float  "5.73628\u0000"
                          before amp 0
                          amp multiplier 10
                          msg as qstring to double 0
                          msg as qstring conv "0"
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #13

                          @Dean21 said in converting from float or double to QString results in output being 0:

                          msg_as_Qstring.remove(("[\u0000]."));

                          How/what do you expect it to match with that . at the end?

                          Just chop off the last character, no regular expression, no? Did you find void QString::chop(qsizetype n) ?

                          If you want to find out whether this will make any difference try

                          msg_as_Qstring = "5.73628";
                          

                          first and then you will know!

                          1 Reply Last reply
                          0
                          • Kent-DorfmanK Kent-Dorfman

                            so what is (float)5.7 / (int)10?

                            Therein is the reason you are getting zero!

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #14

                            @Kent-Dorfman said in converting from float or double to QString results in output being 0:

                            so what is (float)5.7 / (int)10?

                            Umm, that would be 0.57(not 0).... ! Dividing a float by an int does not convert to int! :)

                            1 Reply Last reply
                            2
                            • Kent-DorfmanK Offline
                              Kent-DorfmanK Offline
                              Kent-Dorfman
                              wrote on last edited by
                              #15

                              I stand corrected. I was certain the type of the denominator governed the result. Too much time in embedded kernel domain. D'Oh!

                              1 Reply Last reply
                              2
                              • D Dean21

                                @jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part

                                msg_as_Qstring.remove(("[\u0000]."));
                                

                                but it still has it attached on the end here is the output

                                Topic:  "Light_Level" , Msg:  "5.73628\u0000"
                                test value is "5.73628\u0000"
                                Qstring as float  "5.73628\u0000"
                                before amp 0
                                amp multiplier 10
                                msg as qstring to double 0
                                msg as qstring conv "0"
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @Dean21 said in converting from float or double to QString results in output being 0:

                                msg_as_Qstring.remove(("[\u0000]."));

                                Why did you put [,] and dot there?
                                I suggested this:

                                msg_as_Qstring.remove(QChar("\u0000"));
                                

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                D 1 Reply Last reply
                                0
                                • Kent-DorfmanK Offline
                                  Kent-DorfmanK Offline
                                  Kent-Dorfman
                                  wrote on last edited by
                                  #17

                                  if in fact the conversion is being broken by the unicode null terminator then consider an intermediate conversion thru (char*) instead.

                                  QString num(byteArray.data());

                                  since char arrays have no defined length the constructor shoudl use the null as a string terminator, not as part of the string (as is the case when converting from a container that has an internal size field).

                                  1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @Dean21 said in converting from float or double to QString results in output being 0:

                                    msg_as_Qstring.remove(("[\u0000]."));

                                    Why did you put [,] and dot there?
                                    I suggested this:

                                    msg_as_Qstring.remove(QChar("\u0000"));
                                    
                                    D Offline
                                    D Offline
                                    Dean21
                                    wrote on last edited by
                                    #18

                                    @jsulm sorry the "." was a type but i did try both methods and get a lot of errors about invalid conversion

                                    JonBJ 1 Reply Last reply
                                    0
                                    • D Dean21

                                      @jsulm sorry the "." was a type but i did try both methods and get a lot of errors about invalid conversion

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #19

                                      @Dean21
                                      Already said. Don't know why you don't do the following (takes 10 seconds):

                                      1. Try msg_as_Qstring = "5.73628"; and see if it works now. If it does
                                      2. msg_as_Qstring.chop(1);.

                                      Then you will know whether that is the issue....

                                      D 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @Dean21
                                        Already said. Don't know why you don't do the following (takes 10 seconds):

                                        1. Try msg_as_Qstring = "5.73628"; and see if it works now. If it does
                                        2. msg_as_Qstring.chop(1);.

                                        Then you will know whether that is the issue....

                                        D Offline
                                        D Offline
                                        Dean21
                                        wrote on last edited by
                                        #20

                                        @JonB I just did sorry didnt reply sooner I had to wait this worked great, thank you for your help and everyone else who helped me with this

                                        JonBJ 1 Reply Last reply
                                        1
                                        • D Dean21

                                          @JonB I just did sorry didnt reply sooner I had to wait this worked great, thank you for your help and everyone else who helped me with this

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by
                                          #21

                                          @Dean21
                                          Glad it's working for you now. That's the steps I would have taken to get it working.

                                          @Kent-Dorfman said earlier

                                          I question whether removing non-numeric char at end of QSTring is necessary for the numeric conversion problem since all the conversion code I'm familiar with scans digits until it finds an char that can't be part of the number, and then returns everything up to that point.

                                          But, for whatever reason, conversions to numeric from QString state:

                                          Warning: The QString content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error.

                                          So that's how they do it --- all characters in the string must be valid. You could use:

                                          bool ok;
                                          double val = msg_as_Qstring.toDouble(&ok);
                                          if (!ok)
                                              qDebug() << "Conversion failed!";
                                          
                                          D 1 Reply Last reply
                                          1

                                          • Login

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