What's the best approach to update UI from another class?
-
I know that this question is very old but I cannot figure out how to deal with my problem.
I have a QStackedWidget with two widgets inside. Currently the code for both of them is contained in mainwindow.cpp and it is huge ... What I want to do is separate it into two files - one for each subwidget in QStackedWidget. What's the best way to do it considering each widget accesses the UI and the whole UI is set up in ui->setupUi(this)?
-
I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?
@aiphae said in What's the best approach to update UI from another class?:
I have two designed widgets in the QStackedWidget
Move them out to two ui files, instantiate them in the main window and add them to your stack widget programatically.
-
Don't put those two widgets into the stacked widget in the designer but handle them separately and instantiate those instances in the ctor of your mainwindow and add them to the stack widget.
-
You can create the ui in designer as normal.
-
I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?
@aiphae said in What's the best approach to update UI from another class?:
I have two designed widgets in the QStackedWidget
Move them out to two ui files, instantiate them in the main window and add them to your stack widget programatically.
-
I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?
-
Okay so I did as you said but I cannot interact with my widget ...
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connectDb(); auto emailsPage = new EmailsWidget(this); ui->stackedWidget->addWidget(emailsPage); emailsPage->show(); }
-
Okay so I did as you said but I cannot interact with my widget ...
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connectDb(); auto emailsPage = new EmailsWidget(this); ui->stackedWidget->addWidget(emailsPage); emailsPage->show(); }
@aiphae said in What's the best approach to update UI from another class?:
but I cannot interact with my widget ...
As @Pl45m4 already wrote - use signals and slots to pass data between the mainwindow and the subwindow-
-