I have a two classes with two ui's in QT the first UI called page 1 has a radiobutton…
- User
I have a two classes with two ui's in QT the first UI called page 1 has a radiobutton called shark when shark is selected the user clicks on the next pushbutton which navigates to page2.ui on page 2 UI there is also a radiobutton called sharktwo when sharktwo is selected the user must click a pushbutton called submit which will display the selected count of page1.ui and page2.ui in a label on page2.ui all of this in C++. below is my code for first class and second class. please modify the code
#include "page1.h" #include "./ui_page1.h" page1::page1(QWidget *parent) : QMainWindow(parent) , ui(new Ui::page1) { ui->setupUi(this); }
page1::~page1() { delete ui; }
int page1::getSelectedSharkRadioButtons() { int Shark = 0; if (ui->radioButton->isChecked()) Shark++; return Shark; } void page1::on_pushButton_clicked() { hide(); page2 Page2; Page2.setModal(true); Page2.exec();
}
#include "page2.h" #include "ui_page2.h"
page2::page2(QWidget *parent) : QDialog(parent), ui(new Ui::page2) { ui->setupUi(this); }
page2::~page2() { delete ui; }
int page2::getSelectedSharkRadioButtons_page2() { int Shark_2 = 0; if (ui->radioButton->isChecked()) Shark_2++; return Shark_2; }
- Assistant
Here's the modified code. The main change is that you need to pass the
page1object topage2in order to have access to the selected count from it. I also changed the variable and method names to follow camelCase convention.page1.h: No change
page1.cpp:
#include "page1.h" #include "./ui_page1.h" page1::page1(QWidget *parent) : QMainWindow(parent) , ui(new Ui::page1) { ui->setupUi(this); } page1::~page1() { delete ui; } int page1::getSelectedSharkRadioButtons() { int sharkCount = 0; if (ui->radioButton->isChecked()) sharkCount++; return sharkCount; } void page1::on_pushButton_clicked() { hide(); page2 Page2(nullptr, this); // Pass this page1 object to page2 Page2.setModal(true); Page2.exec(); show(); }