How to link a bool variable to a push button in c++ qt?
-
@JonB Thank you for replying. I don't understand exactly what to do. Do you mean I must run the code in debugging mode and see if there are errors or not?
I ran the code in debugging mode and there were no errors.wrote on 23 Sept 2020, 09:13 last edited by JonB@nanor
Put lines likeqDebug() << "Got to this line (1)"
(you may need#include <QDebug>
at the top of your file) into youron_pushButton_clicked()
&on_pushButton_2_clicked()
slots. When you run from Qt Creator, do you see output appearing (I think it's in the "Application Output" tab at the bottom)? -
@nanor
Put lines likeqDebug() << "Got to this line (1)"
(you may need#include <QDebug>
at the top of your file) into youron_pushButton_clicked()
&on_pushButton_2_clicked()
slots. When you run from Qt Creator, do you see output appearing (I think it's in the "Application Output" tab at the bottom)?wrote on 23 Sept 2020, 09:27 last edited by@JonB I put qDebug() << "1" into on_pushButton_clicked() and qDebug() << "2"; into on_pushButton_2_clicked() . I see outputs when I run the code.
The code works well (after clicking on the pushButton , the labels become unhide and after clicking the pushButton_2(which is related to the multiply of two numbers) it works well too).
The only part that does not work, is when I click on the pushButton for the second time to hide the labels again(When I click on the pushButton for the second time, all the labels remain, but my goal is to hide them again, but nothing happens). -
@JonB I put qDebug() << "1" into on_pushButton_clicked() and qDebug() << "2"; into on_pushButton_2_clicked() . I see outputs when I run the code.
The code works well (after clicking on the pushButton , the labels become unhide and after clicking the pushButton_2(which is related to the multiply of two numbers) it works well too).
The only part that does not work, is when I click on the pushButton for the second time to hide the labels again(When I click on the pushButton for the second time, all the labels remain, but my goal is to hide them again, but nothing happens).wrote on 23 Sept 2020, 09:33 last edited by@nanor said in How to link a bool variable to a push button in c++ qt?:
When I click on the pushButton for the second time, all the labels remain, but my goal is to hide them again, but nothing happen
So look at your code in
on_pushButton_clicked()
where you callsetVisible(true)
on each widget. So why do you expect them to disappear? What could you write there instead oftrue
there to make it work as you want? -
@nanor said in How to link a bool variable to a push button in c++ qt?:
When I click on the pushButton for the second time, all the labels remain, but my goal is to hide them again, but nothing happen
So look at your code in
on_pushButton_clicked()
where you callsetVisible(true)
on each widget. So why do you expect them to disappear? What could you write there instead oftrue
there to make it work as you want?wrote on 23 Sept 2020, 09:44 last edited by@JonB Yes, that's right. But I wrote bForButton = !bForButton; before writing the setvisible(true) for the items in on_pushbutton_clicked(). As in the constructor I wrote bForButton = false, I thought that for the second time clicking on the pushbutton, the boolian value turns into false again and all of the elements become hide again.
-
@JonB Yes, that's right. But I wrote bForButton = !bForButton; before writing the setvisible(true) for the items in on_pushbutton_clicked(). As in the constructor I wrote bForButton = false, I thought that for the second time clicking on the pushbutton, the boolian value turns into false again and all of the elements become hide again.
-
@JonB Yes, that's right. But I wrote bForButton = !bForButton; before writing the setvisible(true) for the items in on_pushbutton_clicked(). As in the constructor I wrote bForButton = false, I thought that for the second time clicking on the pushbutton, the boolian value turns into false again and all of the elements become hide again.
wrote on 23 Sept 2020, 09:57 last edited by JonB@nanor said in How to link a bool variable to a push button in c++ qt?:
But I wrote bForButton = !bForButton
Think about how/why that then messes things up from then on, because you have changed the value of the
bForButton
value in doing so. You must not change that variable itself. Hint: it's OK to pass!bForButton
as an expression/parameter, but not to gobForButton = !bForButton
.... Or, you could choose to go down @Bob64's "current widget visibility" route, though that's more code to write/maintain. -
@nanor but what is using
bForButton
?Actually it is better not to use this flag at all. Can you query the button's current visibility?
wrote on 23 Sept 2020, 10:12 last edited by@Bob64 Actually I used bForButton to hide and unhide the labels by clicking on push buttons (For the first time clicking on the push_button, I want to unhide the labels, for the second time clicking on the push_button I want to hide them again, for the third time the goal is unhiding them again...).
I set bForButton=false for hiding and bForButton=!bForButton for unhiding them. -
@nanor said in How to link a bool variable to a push button in c++ qt?:
But I wrote bForButton = !bForButton
Think about how/why that then messes things up from then on, because you have changed the value of the
bForButton
value in doing so. You must not change that variable itself. Hint: it's OK to pass!bForButton
as an expression/parameter, but not to gobForButton = !bForButton
.... Or, you could choose to go down @Bob64's "current widget visibility" route, though that's more code to write/maintain. -
@JonB I put setVisible(bForButton) instead of setVisible(true) for the elements into on_pushButton_clicked() slots and my code worked well. Thank you so much. I really appreciate your help.
-
@JonB I put setVisible(bForButton) instead of setVisible(true) for the elements into on_pushButton_clicked() slots and my code worked well. Thank you so much. I really appreciate your help.
wrote on 23 Sept 2020, 12:07 last edited by@nanor said in How to link a bool variable to a push button in c++ qt?:
my code worked well
great, so please don't forget to mark your post as solved!
13/13