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. Initialize class members with values from checkbox
Forum Updated to NodeBB v4.3 + New Features

Initialize class members with values from checkbox

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 522 Views 2 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.
  • D Offline
    D Offline
    dvd93
    wrote on 2 Jul 2019, 07:57 last edited by
    #1

    I have a class with some private variables and I would like to initialize them depending on some checkbox in the GUI, but I don't know how to access the checkbox inside the class constructor:

    //a.h
    class A {
      private:
         bool value_1;
         bool value_2;
      public:
          A();
    }
    
    //a.cpp
    A::A()
    {
       value_1 =  //value of the checkbox_1
       value_2 =  //value of the checkbox_2
       //etc...
    }
    
    //mainwindow.cpp
    void MainWindow::on_pushButton_clicked()
    {
        A* a_1= new A;
        //doing things...
        delete a_1;
    }
    
    

    If the variables were public I could initialize them in mainwindow.cpp like this:
    value_1 = ui->checkbox_1->isChecked();
    But in a.cpp there is no "ui" object, how can I pass it?

    G 1 Reply Last reply 2 Jul 2019, 08:03
    0
    • D dvd93
      2 Jul 2019, 07:57

      I have a class with some private variables and I would like to initialize them depending on some checkbox in the GUI, but I don't know how to access the checkbox inside the class constructor:

      //a.h
      class A {
        private:
           bool value_1;
           bool value_2;
        public:
            A();
      }
      
      //a.cpp
      A::A()
      {
         value_1 =  //value of the checkbox_1
         value_2 =  //value of the checkbox_2
         //etc...
      }
      
      //mainwindow.cpp
      void MainWindow::on_pushButton_clicked()
      {
          A* a_1= new A;
          //doing things...
          delete a_1;
      }
      
      

      If the variables were public I could initialize them in mainwindow.cpp like this:
      value_1 = ui->checkbox_1->isChecked();
      But in a.cpp there is no "ui" object, how can I pass it?

      G Offline
      G Offline
      Gojir4
      wrote on 2 Jul 2019, 08:03 last edited by
      #2

      @dvd93
      You can either add these values as arguments of your constructor, or make setters to set them after construction:

      class A {
        private:
           bool value_1;
           bool value_2;
        public:
            A();
            A(bool v1, bool v2);
      
            setValue1(bool v1);
            setValue2(bool v2);
      }
      
      //a.cpp
      //Default constructor
      A::A() : value_1(false), value_2(false)
      {   
      }
      
      //Initialization constructor
      A::A(bool v1, bool v2)
          : value_1(v1), value_2(v2)
      {}
      
      A::setValue1(bool v1){
          value_1 = v1;
      }
      A::setValue2(bool v2){
          value_2 = v2;
      }
      //mainwindow.cpp
      void MainWindow::on_pushButton_clicked()
      {
          A* a_1= new A();
          a_1.setValue1(ui->cb1->isChecked());
          a_2.setValue1(ui->cb2->isChecked());
          // OR 
          A* a_1= new A(ui->cb1->isChecked(), ui->cb2->isChecked());
          //doing things...
          delete a_1;
      }
      
      
      D 1 Reply Last reply 2 Jul 2019, 08:18
      4
      • G Gojir4
        2 Jul 2019, 08:03

        @dvd93
        You can either add these values as arguments of your constructor, or make setters to set them after construction:

        class A {
          private:
             bool value_1;
             bool value_2;
          public:
              A();
              A(bool v1, bool v2);
        
              setValue1(bool v1);
              setValue2(bool v2);
        }
        
        //a.cpp
        //Default constructor
        A::A() : value_1(false), value_2(false)
        {   
        }
        
        //Initialization constructor
        A::A(bool v1, bool v2)
            : value_1(v1), value_2(v2)
        {}
        
        A::setValue1(bool v1){
            value_1 = v1;
        }
        A::setValue2(bool v2){
            value_2 = v2;
        }
        //mainwindow.cpp
        void MainWindow::on_pushButton_clicked()
        {
            A* a_1= new A();
            a_1.setValue1(ui->cb1->isChecked());
            a_2.setValue1(ui->cb2->isChecked());
            // OR 
            A* a_1= new A(ui->cb1->isChecked(), ui->cb2->isChecked());
            //doing things...
            delete a_1;
        }
        
        
        D Offline
        D Offline
        dvd93
        wrote on 2 Jul 2019, 08:18 last edited by
        #3

        @Gojir4
        Thank you very much for your reply.
        I have another question: if I have a lot of values (e.g. 20 bools and 10 chars), do I have to pass all 30 arguments (or make 30 setters) or there is a better option?

        J 1 Reply Last reply 2 Jul 2019, 08:26
        0
        • D dvd93
          2 Jul 2019, 08:18

          @Gojir4
          Thank you very much for your reply.
          I have another question: if I have a lot of values (e.g. 20 bools and 10 chars), do I have to pass all 30 arguments (or make 30 setters) or there is a better option?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 2 Jul 2019, 08:26 last edited by
          #4

          @dvd93 Do you want to initialise all these 20/10 members with same values? If so there is no need to pass so many parameters. If you have to initialise them with different values then you have to pass all the parameters.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 2 Jul 2019, 08:30
          4
          • J jsulm
            2 Jul 2019, 08:26

            @dvd93 Do you want to initialise all these 20/10 members with same values? If so there is no need to pass so many parameters. If you have to initialise them with different values then you have to pass all the parameters.

            D Offline
            D Offline
            dvd93
            wrote on 2 Jul 2019, 08:30 last edited by
            #5

            @jsulm
            yes I want to initialize all of them with the values from the checkbox/spinbox/textedit set in the GUI

            J 1 Reply Last reply 2 Jul 2019, 08:33
            0
            • D dvd93
              2 Jul 2019, 08:30

              @jsulm
              yes I want to initialize all of them with the values from the checkbox/spinbox/textedit set in the GUI

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 2 Jul 2019, 08:33 last edited by
              #6

              @dvd93 Well, then you have to pass them as parameters.
              Or you can create setter methods, one for each member you want to initialise.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              4

              4/6

              2 Jul 2019, 08:26

              • Login

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