[英]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: 我使用这种模式:
QStackedWidget
in mainwindow.cpp 将所有小部件对象添加到QStackedWidget
中的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.