简体   繁体   English

在Qt中处理多个窗口

[英]Handling multiple windows in Qt

I use QStackedWidget to handle multiple windows/forms in a Qt application according to this question . 根据这个问题,我使用QStackedWidget处理Qt应用程序中的多个窗口/窗体。

I use this pattern: 我使用这种模式:

  1. Add all widget objects to the QStackedWidget in mainwindow.cpp 将所有小部件对象添加到QStackedWidget中的QStackedWidget
  2. Get signal from sub-window on request to change window 根据要求从子窗口获取信号以更改窗口
  3. mainwindow replaces the window (it updates the QStackedWidget in the right slot) mainwindow替换窗口(它将更新右侧插槽中的QStackedWidget)

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.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 : 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);
}

QStackedWidget是处理多窗口的好方法。为什么不将按钮放在mainWindow中,以便您可以更方便地更改pagesWidget的currentWidget而不是创建信号槽

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM