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. Comparing QString with int value

Comparing QString with int value

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 549 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by Swati777999
    #1

    Below is the code I wrote

    QString aStr ='4';
    if (aStr > 5)
      qDebug()<<"Condition-1";
    else if (aStr<5)
       qDebug()<<"Condition-2";
    else 
    qDebug()<<"Condition-3";
    

    I took the following test cases for this code ;
    CASE-1 QString aStr='4';
    CASE-2 QString aStr='5';
    CASE-3 QString aStr='8';

    For all the above cases, I got same result as "Condition-1".
    Can someone explain me the reason and how to get the desired output?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    JKSHJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      Below is the code I wrote

      QString aStr ='4';
      if (aStr > 5)
        qDebug()<<"Condition-1";
      else if (aStr<5)
         qDebug()<<"Condition-2";
      else 
      qDebug()<<"Condition-3";
      

      I took the following test cases for this code ;
      CASE-1 QString aStr='4';
      CASE-2 QString aStr='5';
      CASE-3 QString aStr='8';

      For all the above cases, I got same result as "Condition-1".
      Can someone explain me the reason and how to get the desired output?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Swati777999 said in Comparing QString with int value:

      Can someone explain me the reason

      Something looks wrong. Are you certain that you have shown us your actual code? Your compiler should not allow you to compare a QString with an int.

      and how to get the desired output?

      Convert your string to an int first.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      Kent-DorfmanK Swati777999S 2 Replies Last reply
      2
      • JKSHJ JKSH

        @Swati777999 said in Comparing QString with int value:

        Can someone explain me the reason

        Something looks wrong. Are you certain that you have shown us your actual code? Your compiler should not allow you to compare a QString with an int.

        and how to get the desired output?

        Convert your string to an int first.

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

        @JKSH

        If I had to guess, the operator overload

        bool 	operator>(const char *other) const
        

        Is probably to blame. dont know what kind of warnings OP has enabled/disabled, and in its rawest form (5) could be implicitly cast to a pointer, I guess.

        If you meet the AI on the road, kill it.

        1 Reply Last reply
        0
        • JKSHJ JKSH

          @Swati777999 said in Comparing QString with int value:

          Can someone explain me the reason

          Something looks wrong. Are you certain that you have shown us your actual code? Your compiler should not allow you to compare a QString with an int.

          and how to get the desired output?

          Convert your string to an int first.

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on last edited by
          #4

          @JKSH

          yes, but how ? QString::toInt() is not working in this case for me.

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          jsulmJ 1 Reply Last reply
          0
          • Swati777999S Swati777999

            @JKSH

            yes, but how ? QString::toInt() is not working in this case for me.

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

            @Swati777999 said in Comparing QString with int value:

            QString::toInt() is not working in this case for me

            Why not?
            What happens?

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @Swati777999 said in Comparing QString with int value:

              QString aStr ='4';

              This doesn't compile for me, there is no QString constructor accepting a char.
              If you want a plain-old-char-string then:

              QString aStr = "4";
              

              As expected, aStr.toInt() works just fine.

              QString aStr = "4";
              if (aStr.toInt() > 5)
                qDebug()<<"Condition-1";
              else if (aStr.toInt() < 5)
                 qDebug()<<"Condition-2";
              else
              qDebug()<<"Condition-3";
              
              qDebug() << aStr.toInt();
              

              Output:

              Condition-2
              4
              
              1 Reply Last reply
              3

              • Login

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