I want to set this interval [90,100] with && in if condition but it doesn't work!
-
the most common reason obvious code isn't executed in an event driven framework is that the programmer isn't thinking in the event driven model, and no signal is driving the function that does the work. Second most common is that they wrote code that is blocking/stalling the event loop.
-
Hello;
why it doesn't read this condition!if ((MySpeed >= 90) && (MySpeed <= 110)) {mModel->item(b, 6)->setText("+100");}
-
@JonB;
MySpeed "101.95" is QStringif((Send_Frame_Get_ACK_Function(SetPayloadRPM))=="FF") { qDebug() << " Pass" ; MySpeed = Send_Frame_Get_Speed_Function(NSetPayload); //SetPayloadMem="F504" qDebug() << " MySpeed" <<MySpeed; // const uint MSpeed = MySpeed.toUInt(nullptr, 16); if ((MySpeed >= "40") && (MySpeed <= "60") ) {mModel->item(b, 6)->setText("+50");}//result else if ((MySpeed >= "90") && (MySpeed <= "110")) {mModel->item(b, 6)->setText("+100");}//result else {mModel->item(b, 6)->setText(MySpeed);}//result LastSpeed=MySpeed; } else qDebug() << " Fail to get speed " ; }
it gives me in turn +50 insted of +100 in the table view
-
@JonB;
MySpeed "101.95" is QStringif((Send_Frame_Get_ACK_Function(SetPayloadRPM))=="FF") { qDebug() << " Pass" ; MySpeed = Send_Frame_Get_Speed_Function(NSetPayload); //SetPayloadMem="F504" qDebug() << " MySpeed" <<MySpeed; // const uint MSpeed = MySpeed.toUInt(nullptr, 16); if ((MySpeed >= "40") && (MySpeed <= "60") ) {mModel->item(b, 6)->setText("+50");}//result else if ((MySpeed >= "90") && (MySpeed <= "110")) {mModel->item(b, 6)->setText("+100");}//result else {mModel->item(b, 6)->setText(MySpeed);}//result LastSpeed=MySpeed; } else qDebug() << " Fail to get speed " ; }
it gives me in turn +50 insted of +100 in the table view
@imene
Then(MySpeed >= "40") && (MySpeed <= "60")
is true (orMySpeed
is the string+50
) , as you can and should verify for yourself in a debugger or with aqDebug()
statement rather than asking others.This code is not what you showed originally, as it is doing string comparisons, which is likely not what you intend.....
-
@imene
Then(MySpeed >= "40") && (MySpeed <= "60")
is true (orMySpeed
is the string+50
) , as you can and should verify for yourself in a debugger or with aqDebug()
statement rather than asking others.This code is not what you showed originally, as it is doing string comparisons, which is likely not what you intend.....
further to what @JonB said,
#include <QCoreApplication> #include <QDebug> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QString MySpeed("101.95"); qDebug() << "MySpeed ==" << MySpeed; bool result = ((MySpeed >= "40") && (MySpeed <= "60")); qDebug() << "((MySpeed >= \"40\") && (MySpeed <= \"60\"))" << result; result = ((MySpeed >= "90") && (MySpeed <= "110") ); qDebug() << "((MySpeed >= \"90\") && (MySpeed <= \"110\"))" << result; return 0; }
Output is:
MySpeed == "101.95" ((MySpeed >= "40") && (MySpeed <= "60")) false ((MySpeed >= "90") && (MySpeed <= "110")) false
That is because '1' (Unicode 0x0031) is not greater than either '4' (Unicode 0x0034) or '9' (Unicode 0x0039).
See also the notes in QString::compare() -
@imene
Your output now showsError
, which is what I would expect, not the+50
that you claimed previously, yet you do not say you have changed your code [you have now edited to change that, and completely changed your code, sigh....]. Can you please be consistent in what you report.I told you in my last reply that you would not get the result you expect and why, it's up to you if you want to act on it or ignore it. This is absolutely basic C++ programming, and is the case in many/most other languages.
-
further to what @JonB said,
#include <QCoreApplication> #include <QDebug> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QString MySpeed("101.95"); qDebug() << "MySpeed ==" << MySpeed; bool result = ((MySpeed >= "40") && (MySpeed <= "60")); qDebug() << "((MySpeed >= \"40\") && (MySpeed <= \"60\"))" << result; result = ((MySpeed >= "90") && (MySpeed <= "110") ); qDebug() << "((MySpeed >= \"90\") && (MySpeed <= \"110\"))" << result; return 0; }
Output is:
MySpeed == "101.95" ((MySpeed >= "40") && (MySpeed <= "60")) false ((MySpeed >= "90") && (MySpeed <= "110")) false
That is because '1' (Unicode 0x0031) is not greater than either '4' (Unicode 0x0034) or '9' (Unicode 0x0039).
See also the notes in QString::compare()@ChrisW67
i'm working under the mainWindow.cpp where should i add QCoreApplication app(argc, argv); in main.cpp ?void MainWindow::on_Send_Row_Button_clicked() //R:read , W:write {... if((Send_Frame_Get_ACK_Function(SetPayloadRPM))=="FF") { qDebug() << " Pass" ; MySpeed = Send_Frame_Get_Speed_Function(NSetPayload); //SetPayloadMem="F504" qDebug() << " MySpeed" <<MySpeed; // const uint MSpeed = MySpeed.toUInt(nullptr, 16); bool result = ((MySpeed >= "40") && (MySpeed <= "60")); qDebug() << "((MySpeed >= \"40\") && (MySpeed <= \"60\"))" << result; result = ((MySpeed >= "90") && (MySpeed <= "110") ); qDebug() << "((MySpeed >= \"90\") && (MySpeed <= \"110\"))" << result; //if ((MySpeed > 40) && (MySpeed < 60) ) //{mModel->item(b, 6)->setText("+50");}//result if ((MySpeed > 90) && (MySpeed < 110)) {mModel->item(b, 6)->setText("+100");}//result else {mModel->item(b, 6)->setText("Error"); qDebug() << " MySpeed if" <<MySpeed;}//result LastSpeed=MySpeed; } else qDebug() << " Fail to get speed " ; }... }
main.cpp:
#include "mainwindow.h" #include <QApplication> #include <QLoggingCategory> int main(int argc, char *argv[]) { QLoggingCategory::setFilterRules(QStringLiteral("qt.canbus* = true")); QApplication a(argc, argv); /*csv fusion*/ a.setStyle("fusion"); MainWindow w; w.show(); return a.exec(); }
-
@ChrisW67
i'm working under the mainWindow.cpp where should i add QCoreApplication app(argc, argv); in main.cpp ?void MainWindow::on_Send_Row_Button_clicked() //R:read , W:write {... if((Send_Frame_Get_ACK_Function(SetPayloadRPM))=="FF") { qDebug() << " Pass" ; MySpeed = Send_Frame_Get_Speed_Function(NSetPayload); //SetPayloadMem="F504" qDebug() << " MySpeed" <<MySpeed; // const uint MSpeed = MySpeed.toUInt(nullptr, 16); bool result = ((MySpeed >= "40") && (MySpeed <= "60")); qDebug() << "((MySpeed >= \"40\") && (MySpeed <= \"60\"))" << result; result = ((MySpeed >= "90") && (MySpeed <= "110") ); qDebug() << "((MySpeed >= \"90\") && (MySpeed <= \"110\"))" << result; //if ((MySpeed > 40) && (MySpeed < 60) ) //{mModel->item(b, 6)->setText("+50");}//result if ((MySpeed > 90) && (MySpeed < 110)) {mModel->item(b, 6)->setText("+100");}//result else {mModel->item(b, 6)->setText("Error"); qDebug() << " MySpeed if" <<MySpeed;}//result LastSpeed=MySpeed; } else qDebug() << " Fail to get speed " ; }... }
main.cpp:
#include "mainwindow.h" #include <QApplication> #include <QLoggingCategory> int main(int argc, char *argv[]) { QLoggingCategory::setFilterRules(QStringLiteral("qt.canbus* = true")); QApplication a(argc, argv); /*csv fusion*/ a.setStyle("fusion"); MainWindow w; w.show(); return a.exec(); }
-
@imene
Your output now showsError
, which is what I would expect, not the+50
that you claimed previously, yet you do not say you have changed your code [you have now edited to change that, and completely changed your code, sigh....]. Can you please be consistent in what you report.I told you in my last reply that you would not get the result you expect and why, it's up to you if you want to act on it or ignore it. This is absolutely basic C++ programming, and is the case in many/most other languages.
@JonB technically, it could work
bool operator<(const QString &s1, const QString &s2)
is a comparison based exclusively on the numeric Unicode values of the characters
//this returns 1 int main(int argc, char *argv[]) { QString s50("50"); QString s90("90"); QString stringToTest("65"); if(stringToTest >= s50 && stringToTest <= s90) return 1; else return -1; }
but it of course falls quickly apart, especially in the op's case, comparing to "xx.yy"
-
@JonB technically, it could work
bool operator<(const QString &s1, const QString &s2)
is a comparison based exclusively on the numeric Unicode values of the characters
//this returns 1 int main(int argc, char *argv[]) { QString s50("50"); QString s90("90"); QString stringToTest("65"); if(stringToTest >= s50 && stringToTest <= s90) return 1; else return -1; }
but it of course falls quickly apart, especially in the op's case, comparing to "xx.yy"
-
@J-Hilk
Huh? IfstringToTest
is e.g."666"
this returns true, do you really think that is what OP intends?? No offence, but you are confusing the OP here, aren't you?I know, but I'm technically correct, the best kind of correct!
-
@imene
Then(MySpeed >= "40") && (MySpeed <= "60")
is true (orMySpeed
is the string+50
) , as you can and should verify for yourself in a debugger or with aqDebug()
statement rather than asking others.This code is not what you showed originally, as it is doing string comparisons, which is likely not what you intend.....
@J-Hilk
@JonB said in I want to set this interval [90,100] with && in if condition but it doesn't work!:
[...] as it is doing string comparisons, which is likely not what you intend.....
Note "intend". Encouraging OP to think he can/should do it with strings won't get him to the direction he (presumably) needs.....
-
@imene
All of this is quite irrelevant. If you need to compare numbers you need to be comparing numbers not strings or characters, per first answer..... Think about what type you have inMySpeed
, and do something about it.