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
QtWS25 Last Chance

Initialize class members with values from checkbox

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 508 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.
  • D Offline
    D Offline
    dvd93
    wrote on 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?

    Gojir4G 1 Reply Last reply
    0
    • D dvd93

      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?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on 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
      4
      • Gojir4G Gojir4

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

        jsulmJ 1 Reply Last reply
        0
        • D dvd93

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          4
          • jsulmJ jsulm

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

            jsulmJ 1 Reply Last reply
            0
            • D dvd93

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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

              • Login

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