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. How do I check whether a button is disabled in an if statement?
Forum Updated to NodeBB v4.3 + New Features

How do I check whether a button is disabled in an if statement?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 7.7k Views 3 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.
  • D Offline
    D Offline
    Dextank
    wrote on 12 Jan 2020, 02:13 last edited by
    #1

    I have searched around a lot but nothing seems to work

    if(ui->Attack->isEnabled() == false && ui->Attack_2->isEnabled() == false && ui->Attack_3->isEnabled() == false && ui->Flame->isEnabled() == false && ui->Flame_2->isEnabled() == false && ui->Ice->isEnabled() == false && ui->Ice_2->isEnabled() == false && ui->AttackUP->isEnabled() == false && ui->DefenceUP->isEnabled() == false && ui->Wind->isEnabled() == false && ui->Heal->isEnabled() == false && ui->SteelX->isEnabled() == false && ui->BurstKing->isEnabled() == false && ui->Longinus->isEnabled() == false && ui->GearHell->isEnabled() ){
          QMovie *movie20 = new QMovie("C:/Users/punch/Desktop/RST ICS4U/EAttack.gif");
          QLabel *processLabel20 = new QLabel(this);
          processLabel20->setMovie(movie20);
          processLabel20->setGeometry(100,240,481,702);
          processLabel20->show();
          movie20->start();
          QTimer::singleShot(1400, [processLabel20] {
                  processLabel20->close();
                  processLabel20->deleteLater();
          });
      }
    

    This code does not give any errors but the movie does not show up when all specified buttons are disabled. How do I fix this?

    P 1 Reply Last reply 12 Jan 2020, 02:22
    0
    • D Dextank
      12 Jan 2020, 02:13

      I have searched around a lot but nothing seems to work

      if(ui->Attack->isEnabled() == false && ui->Attack_2->isEnabled() == false && ui->Attack_3->isEnabled() == false && ui->Flame->isEnabled() == false && ui->Flame_2->isEnabled() == false && ui->Ice->isEnabled() == false && ui->Ice_2->isEnabled() == false && ui->AttackUP->isEnabled() == false && ui->DefenceUP->isEnabled() == false && ui->Wind->isEnabled() == false && ui->Heal->isEnabled() == false && ui->SteelX->isEnabled() == false && ui->BurstKing->isEnabled() == false && ui->Longinus->isEnabled() == false && ui->GearHell->isEnabled() ){
            QMovie *movie20 = new QMovie("C:/Users/punch/Desktop/RST ICS4U/EAttack.gif");
            QLabel *processLabel20 = new QLabel(this);
            processLabel20->setMovie(movie20);
            processLabel20->setGeometry(100,240,481,702);
            processLabel20->show();
            movie20->start();
            QTimer::singleShot(1400, [processLabel20] {
                    processLabel20->close();
                    processLabel20->deleteLater();
            });
        }
      

      This code does not give any errors but the movie does not show up when all specified buttons are disabled. How do I fix this?

      P Offline
      P Offline
      Pl45m4
      wrote on 12 Jan 2020, 02:22 last edited by
      #2

      @Dextank

      Your last enabled-check has no == false?! Is that intended?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      D 1 Reply Last reply 12 Jan 2020, 02:26
      2
      • P Pl45m4
        12 Jan 2020, 02:22

        @Dextank

        Your last enabled-check has no == false?! Is that intended?

        D Offline
        D Offline
        Dextank
        wrote on 12 Jan 2020, 02:26 last edited by
        #3

        @Pl45m4 oh wow, my bad, this should work though if i put that last false statement, right?

        P 1 Reply Last reply 12 Jan 2020, 02:48
        0
        • D Dextank
          12 Jan 2020, 02:26

          @Pl45m4 oh wow, my bad, this should work though if i put that last false statement, right?

          P Offline
          P Offline
          Pl45m4
          wrote on 12 Jan 2020, 02:48 last edited by
          #4

          @Dextank

          That doesnt look wrong, except that there is probably a cleaner solution to avoid this huge if-clause.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          D 1 Reply Last reply 12 Jan 2020, 03:49
          3
          • P Pl45m4
            12 Jan 2020, 02:48

            @Dextank

            That doesnt look wrong, except that there is probably a cleaner solution to avoid this huge if-clause.

            D Offline
            D Offline
            Dextank
            wrote on 12 Jan 2020, 03:49 last edited by
            #5

            @Pl45m4 It still does not work even after fixing it, do you see anything else wrong with it?

            P 1 Reply Last reply 12 Jan 2020, 04:02
            0
            • D Dextank
              12 Jan 2020, 03:49

              @Pl45m4 It still does not work even after fixing it, do you see anything else wrong with it?

              P Offline
              P Offline
              Pl45m4
              wrote on 12 Jan 2020, 04:02 last edited by
              #6

              @Dextank

              Are all these buttons actually disabled (grey), when you arrive at this point of your code? Test it with one button only if your video starts.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              D 1 Reply Last reply 12 Jan 2020, 05:11
              2
              • P Pl45m4
                12 Jan 2020, 04:02

                @Dextank

                Are all these buttons actually disabled (grey), when you arrive at this point of your code? Test it with one button only if your video starts.

                D Offline
                D Offline
                Dextank
                wrote on 12 Jan 2020, 05:11 last edited by
                #7

                @Pl45m4 It does not work even if I just do one button, the video plays and I tested that it the code. I've also tried messing around with the placement of the code and it seems to work if I put the if statement in one of the button's functions. However, this only works if I click that specific button, I want it to work throughout my entire program.

                M P 2 Replies Last reply 12 Jan 2020, 09:54
                0
                • D Dextank
                  12 Jan 2020, 05:11

                  @Pl45m4 It does not work even if I just do one button, the video plays and I tested that it the code. I've also tried messing around with the placement of the code and it seems to work if I put the if statement in one of the button's functions. However, this only works if I click that specific button, I want it to work throughout my entire program.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 12 Jan 2020, 09:54 last edited by
                  #8

                  @Dextank
                  Hi and welcome to the forums

                  Ok if the same code works in a button slot function
                  then it can find the gif file and the actual code works.

                  Then How do you call that piece of code ?

                  For every other button, when disabled, it/you should call that code to check
                  if to play the movie.

                  Can you show - how you disable one of the buttons and how
                  you call the code you shown here ?

                  1 Reply Last reply
                  1
                  • D Dextank
                    12 Jan 2020, 05:11

                    @Pl45m4 It does not work even if I just do one button, the video plays and I tested that it the code. I've also tried messing around with the placement of the code and it seems to work if I put the if statement in one of the button's functions. However, this only works if I click that specific button, I want it to work throughout my entire program.

                    P Offline
                    P Offline
                    Pl45m4
                    wrote on 12 Jan 2020, 16:19 last edited by Pl45m4 1 Dec 2020, 16:27
                    #9

                    @Dextank said in How do I check whether a button is disabled in an if statement?:

                    I want it to work throughout my entire program

                    If you want to start the video as soon as (and every time) all of your buttons are disabled, you can send a custom signal to a slot, where you put your if-clause. Then emit this signal every time after you disable one of these buttons in your code. This will lead to your if-clause, check whether the if-statement is true or not and your video should start playing afterwards.

                    @Dextank said in How do I check whether a button is disabled in an if statement?:

                    It does not work even if I just do one button, the video plays and I tested that it the code.

                    Of course this cannot work, if you disable buttons on runtime and the program already went through your if-clause (with some of the buttons still enabled).


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    D 1 Reply Last reply 12 Jan 2020, 16:58
                    2
                    • P Pl45m4
                      12 Jan 2020, 16:19

                      @Dextank said in How do I check whether a button is disabled in an if statement?:

                      I want it to work throughout my entire program

                      If you want to start the video as soon as (and every time) all of your buttons are disabled, you can send a custom signal to a slot, where you put your if-clause. Then emit this signal every time after you disable one of these buttons in your code. This will lead to your if-clause, check whether the if-statement is true or not and your video should start playing afterwards.

                      @Dextank said in How do I check whether a button is disabled in an if statement?:

                      It does not work even if I just do one button, the video plays and I tested that it the code.

                      Of course this cannot work, if you disable buttons on runtime and the program already went through your if-clause (with some of the buttons still enabled).

                      D Offline
                      D Offline
                      Dextank
                      wrote on 12 Jan 2020, 16:58 last edited by
                      #10

                      @Pl45m4 Ok, so i looked at what you wrote and I managed to fix it by putting the if statement in every single function for every button that gets disabled. I'm not entirely sure if this is what you wanted me to do or if it is an ideal solution but I'll mark this topic as solved anyway. Thank you for your help.

                      P 1 Reply Last reply 12 Jan 2020, 17:49
                      0
                      • D Dextank
                        12 Jan 2020, 16:58

                        @Pl45m4 Ok, so i looked at what you wrote and I managed to fix it by putting the if statement in every single function for every button that gets disabled. I'm not entirely sure if this is what you wanted me to do or if it is an ideal solution but I'll mark this topic as solved anyway. Thank you for your help.

                        P Offline
                        P Offline
                        Pl45m4
                        wrote on 12 Jan 2020, 17:49 last edited by Pl45m4 1 Dec 2020, 17:49
                        #11

                        @Dextank

                        Not 100% :)
                        You have many lines of duplicate code now. Are you aware of the Signal & Slot mechanism in Qt?
                        You can keep your if-statement in one function (slot) and call it (by emiting a signal) whenever one of your buttons gets disabled.


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        D 1 Reply Last reply 12 Jan 2020, 18:35
                        1
                        • P Pl45m4
                          12 Jan 2020, 17:49

                          @Dextank

                          Not 100% :)
                          You have many lines of duplicate code now. Are you aware of the Signal & Slot mechanism in Qt?
                          You can keep your if-statement in one function (slot) and call it (by emiting a signal) whenever one of your buttons gets disabled.

                          D Offline
                          D Offline
                          Dextank
                          wrote on 12 Jan 2020, 18:35 last edited by
                          #12

                          @Pl45m4 So basically you want me to make another function with the if statement in it and then connect all my buttons to that function? This would mean that all my buttons now have two functions connected to them, is this alright?

                          P 1 Reply Last reply 12 Jan 2020, 23:23
                          0
                          • D Dextank
                            12 Jan 2020, 18:35

                            @Pl45m4 So basically you want me to make another function with the if statement in it and then connect all my buttons to that function? This would mean that all my buttons now have two functions connected to them, is this alright?

                            P Offline
                            P Offline
                            Pl45m4
                            wrote on 12 Jan 2020, 23:23 last edited by Pl45m4 1 Dec 2020, 23:23
                            #13

                            @Dextank said in How do I check whether a button is disabled in an if statement?:

                            So basically you want me to make another function with the if statement in it and then connect all my buttons to that function?

                            I want nothing :-) Do what you want :-) But you asked for help. You can keep your solution if you want to, but, as I said, it's not the cleanest one.

                            @Dextank said in How do I check whether a button is disabled in an if statement?:

                            This would mean that all my buttons now have two functions connected to them, is this alright?

                            Why two?

                            This is how *I* would do it:

                            // Your class header (eg. MainWindow)
                            
                            signals:
                                   void btnDisabled();
                            
                            public slots:
                                   void playVideo();
                            
                            // Constructor
                            {
                            
                                ui->setupUi(this);
                                connect(this, &MainWindow::btnDisabled, this, &MainWindow::playVideo);
                            }
                            
                            
                            void MainWindow::randomFnct()
                            {
                               // Some code
                               
                               // eg. one of your buttons gets disabled here
                               ui->randomButton->setDisabled(true);
                               // emit signal afterwards
                               emit btnDisabled();
                            }
                            
                            void MainWindow::playVideo()
                            {
                               // your If-clause here
                            }
                            

                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            D 1 Reply Last reply 13 Jan 2020, 00:01
                            2
                            • P Pl45m4
                              12 Jan 2020, 23:23

                              @Dextank said in How do I check whether a button is disabled in an if statement?:

                              So basically you want me to make another function with the if statement in it and then connect all my buttons to that function?

                              I want nothing :-) Do what you want :-) But you asked for help. You can keep your solution if you want to, but, as I said, it's not the cleanest one.

                              @Dextank said in How do I check whether a button is disabled in an if statement?:

                              This would mean that all my buttons now have two functions connected to them, is this alright?

                              Why two?

                              This is how *I* would do it:

                              // Your class header (eg. MainWindow)
                              
                              signals:
                                     void btnDisabled();
                              
                              public slots:
                                     void playVideo();
                              
                              // Constructor
                              {
                              
                                  ui->setupUi(this);
                                  connect(this, &MainWindow::btnDisabled, this, &MainWindow::playVideo);
                              }
                              
                              
                              void MainWindow::randomFnct()
                              {
                                 // Some code
                                 
                                 // eg. one of your buttons gets disabled here
                                 ui->randomButton->setDisabled(true);
                                 // emit signal afterwards
                                 emit btnDisabled();
                              }
                              
                              void MainWindow::playVideo()
                              {
                                 // your If-clause here
                              }
                              
                              D Offline
                              D Offline
                              Dextank
                              wrote on 13 Jan 2020, 00:01 last edited by
                              #14

                              @Pl45m4 I used your code as a basis for what I wanted to do and it seems to work exactly the way I wanted it to, my program also looks a lot cleaner now. Thanks for all your help.

                              1 Reply Last reply
                              0
                              • Kent-DorfmanK Offline
                                Kent-DorfmanK Offline
                                Kent-Dorfman
                                wrote on 13 Jan 2020, 00:16 last edited by
                                #15

                                One final thought that I didn't see mentioned above.
                                In C++ booleans don't need to (and shouldn't) be directly compared to true/false. It is adequate to say

                                if (something()) { // true
                                    do_this(); 
                                } else if (!something_else()) { // false
                                    do_that(); 
                                }
                                
                                P 1 Reply Last reply 13 Jan 2020, 00:29
                                0
                                • Kent-DorfmanK Kent-Dorfman
                                  13 Jan 2020, 00:16

                                  One final thought that I didn't see mentioned above.
                                  In C++ booleans don't need to (and shouldn't) be directly compared to true/false. It is adequate to say

                                  if (something()) { // true
                                      do_this(); 
                                  } else if (!something_else()) { // false
                                      do_that(); 
                                  }
                                  
                                  P Offline
                                  P Offline
                                  Pl45m4
                                  wrote on 13 Jan 2020, 00:29 last edited by
                                  #16

                                  @Kent-Dorfman said in How do I check whether a button is disabled in an if statement?:

                                  if (something())

                                  something() only if it's a bool function ;-)


                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  1 Reply Last reply
                                  1
                                  • Kent-DorfmanK Offline
                                    Kent-DorfmanK Offline
                                    Kent-Dorfman
                                    wrote on 13 Jan 2020, 00:45 last edited by
                                    #17

                                    @Pl45m4 said in How do I check whether a button is disabled in an if statement?:

                                    something() only if it's a bool function ;-)

                                    Well, sort of...An integer function that returns zero is said to be "false", and (!0 == true). Of course that kind of logic in C++ is highly discouraged, even though it is technically supported.

                                    P 1 Reply Last reply 13 Jan 2020, 00:49
                                    0
                                    • Kent-DorfmanK Kent-Dorfman
                                      13 Jan 2020, 00:45

                                      @Pl45m4 said in How do I check whether a button is disabled in an if statement?:

                                      something() only if it's a bool function ;-)

                                      Well, sort of...An integer function that returns zero is said to be "false", and (!0 == true). Of course that kind of logic in C++ is highly discouraged, even though it is technically supported.

                                      P Offline
                                      P Offline
                                      Pl45m4
                                      wrote on 13 Jan 2020, 00:49 last edited by Pl45m4
                                      #18

                                      @Kent-Dorfman

                                      I meant, when something is a bool something, then it would be if(something){} not if(something( ) ) .


                                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                      ~E. W. Dijkstra

                                      1 Reply Last reply
                                      0

                                      1/18

                                      12 Jan 2020, 02:13

                                      • Login

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