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. I want to set this interval [90,100] with && in if condition but it doesn't work!
Forum Updated to NodeBB v4.3 + New Features

I want to set this interval [90,100] with && in if condition but it doesn't work!

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 923 Views 1 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #3

    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.

    1 Reply Last reply
    0
    • I imene

      Hello;
      why it doesn't read this condition!

      if ((MySpeed >= 90) && (MySpeed <= 110))     
                {mModel->item(b, 6)->setText("+100");}
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @imene
      Your code successfully checks whether MySpeed lies between 90 and 110 inclusive, as you could verify in a debugger. Whether you call it in the right place, with the right value, and whether it then does what you expect, is a different matter.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        imene
        wrote on last edited by
        #5

        @JonB;
        MySpeed "101.95" is QString

         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);
        
                      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
        6ac8e805-54b6-42b8-b11e-723773c2a831-image.png

        JonBJ 1 Reply Last reply
        0
        • I imene

          @JonB;
          MySpeed "101.95" is QString

           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);
          
                        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
          6ac8e805-54b6-42b8-b11e-723773c2a831-image.png

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

          @imene
          Then (MySpeed >= "40") && (MySpeed <= "60") is true (or MySpeed is the string +50) , as you can and should verify for yourself in a debugger or with a qDebug() 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.....

          C JonBJ 2 Replies Last reply
          1
          • I Offline
            I Offline
            imene
            wrote on last edited by imene
            #7

            Guys;
            I've commented first condition and i verified the debugger this is what i get :
            4fd1d31f-2b7f-4085-9f6e-8d209c0e26ac-image.png

            JonBJ 1 Reply Last reply
            0
            • JonBJ JonB

              @imene
              Then (MySpeed >= "40") && (MySpeed <= "60") is true (or MySpeed is the string +50) , as you can and should verify for yourself in a debugger or with a qDebug() 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.....

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #8

              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()

              I 1 Reply Last reply
              0
              • I imene

                Guys;
                I've commented first condition and i verified the debugger this is what i get :
                4fd1d31f-2b7f-4085-9f6e-8d209c0e26ac-image.png

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

                @imene
                Your output now shows Error, 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.

                J.HilkJ 1 Reply Last reply
                1
                • C ChrisW67

                  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()

                  I Offline
                  I Offline
                  imene
                  wrote on last edited by
                  #10

                  @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();
                  }
                  
                  

                  7e9c8a50-d1f0-4b20-b553-54b2c5d97de5-image.png

                  JonBJ 1 Reply Last reply
                  0
                  • I imene

                    @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();
                    }
                    
                    

                    7e9c8a50-d1f0-4b20-b553-54b2c5d97de5-image.png

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

                    @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 in MySpeed, and do something about it.

                    I 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @imene
                      Your output now shows Error, 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.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #12

                      @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"


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      JonBJ 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @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"

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

                        @J-Hilk
                        Huh? If stringToTest 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?

                        J.HilkJ 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @J-Hilk
                          Huh? If stringToTest 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?

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #14

                          I know, but I'm technically correct, the best kind of correct!

                          alt text


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @imene
                            Then (MySpeed >= "40") && (MySpeed <= "60") is true (or MySpeed is the string +50) , as you can and should verify for yourself in a debugger or with a qDebug() 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.....

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

                            @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.....

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              imene
                              wrote on last edited by
                              #16

                              Thanks guys;
                              i solve the problem thanks to you :)
                              making conversion from string to float solve the problem:
                              double MSpeed = MySpeed.toFloat();

                              1 Reply Last reply
                              1
                              • JonBJ JonB

                                @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 in MySpeed, and do something about it.

                                I Offline
                                I Offline
                                imene
                                wrote on last edited by
                                #17

                                @JonB Now i compare two floats and the problem is solved thanks a lot.

                                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