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 to link a bool variable to a push button in c++ qt?
QtWS25 Last Chance

How to link a bool variable to a push button in c++ qt?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 1.7k Views
  • 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.
  • N Offline
    N Offline
    nanor
    wrote on 23 Sept 2020, 08:55 last edited by
    #1

    I am working on a project that after clicking on a push button , some labels and lineedits that were hidden before, become unhide. After that, for the second time clicking on the pushbutton, I want to hide the labels again.

    I have written the following code and used a bool variable, but nothing happened.

    Here is my code:

    dialog.h:

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_pushButton_2_clicked();
    
    private:
        Ui::Dialog *ui;
        bool bForButton ;
    };
    
    #endif // DIALOG_H
    
    

    dialog.cpp:

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        bForButton = false;
        ui->setupUi(this);
        ui->label->setVisible(false);
        ui->label_2->setVisible(false);
        ui->lineEdit->setVisible(false);
        ui->lineEdit_2->setVisible(false);
        ui->label_3->setVisible(false);
        ui->pushButton_2->setVisible(false);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
        bForButton = !bForButton;
    
        ui->label->setVisible(true);
        ui->label_2->setVisible(true);
        ui->lineEdit->setVisible(true);
        ui->lineEdit_2->setVisible(true);
        ui->label_3->setVisible(true);
        ui->pushButton_2->setVisible(true);
    }
    
    void Dialog::on_pushButton_2_clicked()
    {
    
        double a,b,c;
        a = ui->lineEdit->text().toDouble(); //a=doublevalue of textbar1
        b = ui->lineEdit_2->text().toDouble(); //b = doublevalue of textbar2
    
        c=a*b;
        ui->label_3->setText(QString::number(c));
    }
    
    J 1 Reply Last reply 23 Sept 2020, 08:59
    0
    • N nanor
      23 Sept 2020, 08:55

      I am working on a project that after clicking on a push button , some labels and lineedits that were hidden before, become unhide. After that, for the second time clicking on the pushbutton, I want to hide the labels again.

      I have written the following code and used a bool variable, but nothing happened.

      Here is my code:

      dialog.h:

      #ifndef DIALOG_H
      #define DIALOG_H
      
      #include <QDialog>
      
      namespace Ui {
      class Dialog;
      }
      
      class Dialog : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Dialog(QWidget *parent = 0);
          ~Dialog();
      
      private slots:
          void on_pushButton_clicked();
      
          void on_pushButton_2_clicked();
      
      private:
          Ui::Dialog *ui;
          bool bForButton ;
      };
      
      #endif // DIALOG_H
      
      

      dialog.cpp:

      #include "dialog.h"
      #include "ui_dialog.h"
      
      Dialog::Dialog(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Dialog)
      {
          bForButton = false;
          ui->setupUi(this);
          ui->label->setVisible(false);
          ui->label_2->setVisible(false);
          ui->lineEdit->setVisible(false);
          ui->lineEdit_2->setVisible(false);
          ui->label_3->setVisible(false);
          ui->pushButton_2->setVisible(false);
      }
      
      Dialog::~Dialog()
      {
          delete ui;
      }
      
      void Dialog::on_pushButton_clicked()
      {
          bForButton = !bForButton;
      
          ui->label->setVisible(true);
          ui->label_2->setVisible(true);
          ui->lineEdit->setVisible(true);
          ui->lineEdit_2->setVisible(true);
          ui->label_3->setVisible(true);
          ui->pushButton_2->setVisible(true);
      }
      
      void Dialog::on_pushButton_2_clicked()
      {
      
          double a,b,c;
          a = ui->lineEdit->text().toDouble(); //a=doublevalue of textbar1
          b = ui->lineEdit_2->text().toDouble(); //b = doublevalue of textbar2
      
          c=a*b;
          ui->label_3->setText(QString::number(c));
      }
      
      J Offline
      J Offline
      JonB
      wrote on 23 Sept 2020, 08:59 last edited by
      #2

      @nanor
      You seem to be relying on the names of your slots being auto-connected to signals. Put in debug messages to see whether your slots are being called at all?

      N 1 Reply Last reply 23 Sept 2020, 09:08
      0
      • J JonB
        23 Sept 2020, 08:59

        @nanor
        You seem to be relying on the names of your slots being auto-connected to signals. Put in debug messages to see whether your slots are being called at all?

        N Offline
        N Offline
        nanor
        wrote on 23 Sept 2020, 09:08 last edited by
        #3

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

        J 1 Reply Last reply 23 Sept 2020, 09:13
        0
        • N nanor
          23 Sept 2020, 09:08

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

          J Offline
          J Offline
          JonB
          wrote on 23 Sept 2020, 09:13 last edited by JonB
          #4

          @nanor
          Put lines like qDebug() << "Got to this line (1)" (you may need #include <QDebug> at the top of your file) into your on_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)?

          N 1 Reply Last reply 23 Sept 2020, 09:27
          0
          • J JonB
            23 Sept 2020, 09:13

            @nanor
            Put lines like qDebug() << "Got to this line (1)" (you may need #include <QDebug> at the top of your file) into your on_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)?

            N Offline
            N Offline
            nanor
            wrote on 23 Sept 2020, 09:27 last edited by
            #5

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

            J 1 Reply Last reply 23 Sept 2020, 09:33
            0
            • N nanor
              23 Sept 2020, 09:27

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

              J Offline
              J Offline
              JonB
              wrote on 23 Sept 2020, 09:33 last edited by
              #6

              @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 call setVisible(true) on each widget. So why do you expect them to disappear? What could you write there instead of true there to make it work as you want?

              N 1 Reply Last reply 23 Sept 2020, 09:44
              0
              • J JonB
                23 Sept 2020, 09:33

                @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 call setVisible(true) on each widget. So why do you expect them to disappear? What could you write there instead of true there to make it work as you want?

                N Offline
                N Offline
                nanor
                wrote on 23 Sept 2020, 09:44 last edited by
                #7

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

                B J 2 Replies Last reply 23 Sept 2020, 09:51
                0
                • N nanor
                  23 Sept 2020, 09:44

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

                  B Offline
                  B Offline
                  Bob64
                  wrote on 23 Sept 2020, 09:51 last edited by
                  #8

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

                  N 1 Reply Last reply 23 Sept 2020, 10:12
                  0
                  • N nanor
                    23 Sept 2020, 09:44

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 23 Sept 2020, 09:57 last edited by JonB
                    #9

                    @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 go bForButton = !bForButton.... Or, you could choose to go down @Bob64's "current widget visibility" route, though that's more code to write/maintain.

                    N 1 Reply Last reply 23 Sept 2020, 10:23
                    0
                    • B Bob64
                      23 Sept 2020, 09:51

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

                      N Offline
                      N Offline
                      nanor
                      wrote on 23 Sept 2020, 10:12 last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • J JonB
                        23 Sept 2020, 09:57

                        @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 go bForButton = !bForButton.... Or, you could choose to go down @Bob64's "current widget visibility" route, though that's more code to write/maintain.

                        N Offline
                        N Offline
                        nanor
                        wrote on 23 Sept 2020, 10:23 last edited by
                        #11

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

                        J P 2 Replies Last reply 23 Sept 2020, 10:37
                        1
                        • N nanor
                          23 Sept 2020, 10:23

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

                          J Offline
                          J Offline
                          JonB
                          wrote on 23 Sept 2020, 10:37 last edited by
                          #12

                          @nanor
                          Well done for figuring this for yourself! Hopefully you can see now exactly why that is indeed the right thing to do!

                          1 Reply Last reply
                          0
                          • N nanor
                            23 Sept 2020, 10:23

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

                            P Offline
                            P Offline
                            Pablo J. Rogina
                            wrote on 23 Sept 2020, 12:07 last edited by
                            #13

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

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            0

                            3/13

                            23 Sept 2020, 09:08

                            topic:navigator.unread, 10
                            • Login

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