How do I check whether a button is disabled in an if statement?
-
wrote on 12 Jan 2020, 02:13 last edited by
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?
-
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?
-
@Pl45m4 oh wow, my bad, this should work though if i put that last false statement, right?
-
That doesnt look wrong, except that there is probably a cleaner solution to avoid this huge if-clause.
-
@Pl45m4 It still does not work even after fixing it, do you see anything else wrong with it?
-
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.
wrote on 12 Jan 2020, 05:11 last edited by@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.
wrote on 12 Jan 2020, 16:19 last edited by Pl45m4 1 Dec 2020, 16:27@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).
wrote on 12 Jan 2020, 16:58 last edited by@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.
wrote on 12 Jan 2020, 17:49 last edited by Pl45m4 1 Dec 2020, 17:49Not 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.wrote on 12 Jan 2020, 18:35 last edited by@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?
-
@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?
wrote on 12 Jan 2020, 23:23 last edited by Pl45m4 1 Dec 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 }
-
@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 }
-
wrote on 13 Jan 2020, 00:16 last edited by
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(); }
wrote on 13 Jan 2020, 00:29 last edited by@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 ;-) -
wrote on 13 Jan 2020, 00:45 last edited by
@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.
wrote on 13 Jan 2020, 00:49 last edited by Pl45m4I meant, when
something
is abool something
, then it would beif(something){}
notif(something( ) )
.
1/18