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. [SOLVED] IF / ELSE strange behaving
QtWS25 Last Chance

[SOLVED] IF / ELSE strange behaving

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.0k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    So, below is my If/else statement... IF checkbox IS checked, execute code, ELSE set text to label... Unfortunately even when checkbox is not checked, code below executes... Also when I check checkbox, ELSE statement does not turn off text "Box is not checked"...

    @void MainWindow::checkbox()
    {
    if (ui->checkBox->isChecked())
    {

        ui->Play->setEnabled(false);
        if (QTime::currentTime(),(QTime::currentTime() > QTime(9,25,00)) & (QTime::currentTime()< QTime(9,55,00)))
        {
        player->play();
        }
        else{
        player->stop();
        }
    }
    else
    {
        ui->label->setText("Box is not checked!");
    }
    

    }@

    I am using timer for this as well....

    @timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(checkbox()));
    timer->start(1000);@

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NicuPopescu
      wrote on last edited by
      #2

      are you sure that the AND operator "&&" is written right?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        I tried to use double & as well, but it's same... It should not even execute that part of the code as long as checkbox is not checked... But it simply execute everything even though checkbox is not checked...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Your test doesn't do what you think it does. Have a look "here":http://www.cplusplus.com/doc/tutorial/operators/ about using the comma operator (which in your case has nothing to do here). And you are not using the logical and operator but the bitwise which are not the same thing.

          You should rather have something like:
          @
          QTime current = QTimer::currentTime();
          if ((current > QTime(9,25,00)) && (current < QTime(9,55,00)))
          @

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Eww... Missed that part :D Anyways it has exactly the same result... I just realised I posted something that is not problem here ... So tired that I forgot what I actually want...

            Well, I will try to explain... I have a media player and I have two checkboxes as you can see on image... So if one checkbox is checked and time matches start playing a song... That part works well...

            But problem is if both checkboxes are checked... Then none of them will play... checkbox () supposed to start at 9:25 - 9:55 but second one (checkbox1())is executing code player->stop()... So my question is how to make checkbox1() stop executing ELSE statement (player -> stop) when checkbox() is executing and vice versa....

            http://img14.imageshack.us/img14/5720/wrd0.png

            !http://img14.imageshack.us/img14/5720/wrd0.png(img)!

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Solved with else if statement...

              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