簡體   English   中英

在Qt中處理多個窗口

[英]Handling multiple windows in Qt

根據這個問題,我使用QStackedWidget處理Qt應用程序中的多個窗口/窗體。

我使用這種模式:

  1. 將所有小部件對象添加到QStackedWidget中的QStackedWidget
  2. 根據要求從子窗口獲取信號以更改窗口
  3. mainwindow替換窗口(它將更新右側插槽中的QStackedWidget)

我的問題 :

這是正確的方法嗎? 我的應用程序中有很多窗口,並希望確保這是常見的最佳做法。 這種模式意味着我有指向主窗口中所有窗口的指針。

我的一段代碼: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);
}

QStackedWidget是處理多窗口的好方法。為什么不將按鈕放在mainWindow中,以便您可以更方便地更改pagesWidget的currentWidget而不是創建信號槽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM