converting from float or double to QString results in output being 0
-
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 -
@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!
-
@Dean21 said in converting from float or double to QString results in output being 0:
amplification_multiplier
what is the type of this identifier?
-
@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.
-
@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
-
@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? -
so what is (float)5.7 / (int)10?
Therein is the reason you are getting zero!
-
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.
-
@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
-
@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"
-
@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!
-
@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
(not0
).... ! Dividing afloat
by anint
does not convert toint
! :) -
I stand corrected. I was certain the type of the denominator governed the result. Too much time in embedded kernel domain. D'Oh!
-
@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"));
-
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).