Natile
Natile

Reputation: 193

Handling multiple windows in Qt

I use QStackedWidget to handle multiple windows/forms in a Qt application according to this question.

I use this pattern:

  1. Add all widget objects to the QStackedWidget in mainwindow.cpp
  2. Get signal from sub-window on request to change window
  3. mainwindow replaces the window (it updates the QStackedWidget in the right slot)

My question :

is this the right way to do this? I have a lot of windows in my applications and want to ensure this is the common best practice. This pattern means that i have pointers to all of the windows in my main window.

piece of my code: mainwindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{

   ui->setupUi(this);

   mnuWin = new Menu();
   singlePulseWin = new SinglePulse();
   repetitive = new Repetitive();
   treatmentType = new TreatmentType();
   //... and so on ....

   connect(mnuWin,&Menu::updateMainWindowStackWidget,this,&MainWindow::onChangeWindowRequested);
   connect(singlePulseWin,&SinglePulse::updateMainWindowStackWidget,this,&MainWindow::onChangeWindowRequested); 
   connect(repetitive,&Repetitive::updateMainWindowStackWidget,this,&MainWindow::onChangeWindowRequested);
   connect(treatmentType,&TreatmentType::updateMainWindowStackWidget,this,&MainWindow::onChangeWindowRequested);
   //... and so on ....

   ui->pagesWidget->addWidget(mnuWin);
   ui->pagesWidget->addWidget(singlePulseWin);
   ui->pagesWidget->addWidget(repetitive);
   ui->pagesWidget->addWidget(treatmentType);
   //... and so on ....

   ui->pagesWidget->setCurrentIndex(0);
}   

void MainWindow::onChangeWindowRequested(int ind)  //slot
{
    ui->pagesWidget->setCurrentIndex(ind);
}

menu.cpp :

void Menu::on_btnMenuSinglePulse_clicked()
{
   emit updateMainWindowStackWidget(1);
}

void Menu::on_btnMenuRepetitive_clicked()
{
   emit updateMainWindowStackWidget(2);
}

void Menu::on_btnMenuBurst_clicked()
{
   emit updateMainWindowStackWidget(3);
}

Upvotes: 0

Views: 5074

Answers (1)

haris
haris

Reputation: 152

QStackedWidget is a good way to deal with Multi-window .why not put the buttons in the mainWindow ,so that you can change the pagesWidget's currentWidget more convenient instead of create signal-slot

Upvotes: 1

Related Questions