Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Why only condition is working for "if" condition?
Qt 6.11 is out! See what's new in the release blog

Why only condition is working for "if" condition?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 3 Posters 723 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.
  • M Offline
    M Offline
    Mohit Tripathi
    wrote on last edited by
    #1

    Hi,

    I am trying to set the range of integers using Validator and showing the notification message if integer is not within the range. But, I can see that only one condition is working in "if" condition for "data2", not for "data3". I have checked for multiple datas but it is working for only one condtion. Can I know why?

    
        int data2 = ui->lineEdit9_11->text().toInt();
        int data3 = ui->lineEdit10_11->text().toInt();
    
        qDebug()<<data2;
        qDebug()<<data3;
        if(((data2<50||data2>100)&&(data3<0||data3>40) ))
        {
            QMessageBox msg;
            msg.setText("Range should be 50 to 100\n"
                        "Range should be 0 to 40\n");
            msg.exec();
            return;
        }
    
    J.HilkJ KroMignonK 2 Replies Last reply
    0
    • M Mohit Tripathi

      Hi,

      I am trying to set the range of integers using Validator and showing the notification message if integer is not within the range. But, I can see that only one condition is working in "if" condition for "data2", not for "data3". I have checked for multiple datas but it is working for only one condtion. Can I know why?

      
          int data2 = ui->lineEdit9_11->text().toInt();
          int data3 = ui->lineEdit10_11->text().toInt();
      
          qDebug()<<data2;
          qDebug()<<data3;
          if(((data2<50||data2>100)&&(data3<0||data3>40) ))
          {
              QMessageBox msg;
              msg.setText("Range should be 50 to 100\n"
                          "Range should be 0 to 40\n");
              msg.exec();
              return;
          }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Mohit-Tripathi

      change your && to an ||,
      otherwise you only enter the if branch when both, data2 and data3 are out of range

      currently:
      data2 = true && data3 == false -> does not enter
      data2 = false && data3 == true -> does not enter
      data2 = false && data3 == false -> does not enter
      data2 = true && data3 == true -> does enter


      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
      4
      • M Mohit Tripathi

        Hi,

        I am trying to set the range of integers using Validator and showing the notification message if integer is not within the range. But, I can see that only one condition is working in "if" condition for "data2", not for "data3". I have checked for multiple datas but it is working for only one condtion. Can I know why?

        
            int data2 = ui->lineEdit9_11->text().toInt();
            int data3 = ui->lineEdit10_11->text().toInt();
        
            qDebug()<<data2;
            qDebug()<<data3;
            if(((data2<50||data2>100)&&(data3<0||data3>40) ))
            {
                QMessageBox msg;
                msg.setText("Range should be 50 to 100\n"
                            "Range should be 0 to 40\n");
                msg.exec();
                return;
            }
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @Mohit-Tripathi said in Why only condition is working for "if" condition?:

        Can I know why?

        This is simple Boolean algebra:

        False && False ==> False
        False && True  ==> False
        True  && False ==> False
        True  && True  ==> True
        
        False || False ==> False
        False || True  ==> True
        True  || False ==> True
        True  || True  ==> True
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        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