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.
  • Pl45m4P Pl45m4

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

    mrjjM Pl45m4P 2 Replies Last reply
    0
    • D Dextank

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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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

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

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #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
        2
        • Pl45m4P Pl45m4

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

          Pl45m4P 1 Reply Last reply
          0
          • D Dextank

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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #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
            1
            • Pl45m4P Pl45m4

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

              Pl45m4P 1 Reply Last reply
              0
              • D Dextank

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

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #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
                2
                • Pl45m4P Pl45m4

                  @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 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 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(); 
                    }
                    
                    Pl45m4P 1 Reply Last reply
                    0
                    • Kent-DorfmanK Kent-Dorfman

                      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(); 
                      }
                      
                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on 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 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.

                        Pl45m4P 1 Reply Last reply
                        0
                        • Kent-DorfmanK Kent-Dorfman

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

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on 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

                          • Login

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