How do I check whether a button is disabled in an if statement?
-
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.
@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.
-
@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.
@Dextank
Hi and welcome to the forumsOk 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 ? -
@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.
@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).
-
@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).
@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.
-
@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.
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. -
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. -
@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?
@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 }
-
@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 }
-
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 sayif (something()) { // true do_this(); } else if (!something_else()) { // false do_that(); }
-
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 sayif (something()) { // true do_this(); } else if (!something_else()) { // false do_that(); }
@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 ;-) -
@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.
-
@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.
I meant, when
something
is abool something
, then it would beif(something){}
notif(something( ) )
.