Initialize class members with values from checkbox
-
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? -
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?@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; }
-
@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; }
-
@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? -
@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.
-
@jsulm
yes I want to initialize all of them with the values from the checkbox/spinbox/textedit set in the GUI