关闭所有子窗口:
cpp
void QWMainWindow::closeEvent(QCloseEvent *event)
{
ui->mdiArea->closeAllSubWindows(); //关闭所有子窗口
event->accept();
}
新建窗口:
cpp
QFormDoc *formDoc = new QFormDoc(this); //
ui->mdiArea->addSubWindow(formDoc); //文档窗口添加到MDI
formDoc->show(); //在单独的窗口中显示
获取窗口并进行相关操作:
cpp
void QWMainWindow::on_actCut_triggered()
{ //cut
QFormDoc* formDoc=(QFormDoc*)ui->mdiArea->activeSubWindow()->widget();
formDoc->textCut();
}
void QWMainWindow::on_actFont_triggered()
{//设置字体
QFormDoc* formDoc=(QFormDoc*)ui->mdiArea->activeSubWindow()->widget();
formDoc->setEditFont();
}
void QWMainWindow::on_actCopy_triggered()
{//copy
QFormDoc* formDoc=(QFormDoc*)ui->mdiArea->activeSubWindow()->widget();
formDoc->textCopy();
}
void QWMainWindow::on_actPaste_triggered()
{//paste
QFormDoc* formDoc=(QFormDoc*)ui->mdiArea->activeSubWindow()->widget();
formDoc->textPaste();
}
当前活动子窗口切换时:
cpp
void QWMainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{//当前活动子窗口切换时
Q_UNUSED(arg1);
if (ui->mdiArea->subWindowList().count()==0)
{ //若子窗口个数为零
ui->actCut->setEnabled(false);
ui->actCopy->setEnabled(false);
ui->actPaste->setEnabled(false);
ui->actFont->setEnabled(false);
ui->statusBar->clearMessage();
}
else
{
QFormDoc *formDoc=static_cast<QFormDoc*>(ui->mdiArea->activeSubWindow()->widget());
ui->statusBar->showMessage(formDoc->currentFileName()); //显示主窗口的文件名
}
}
模式设置:
cpp
MDI 显示模式
ui->mdiArea->setViewMode(QMdiArea::TabbedView); //Tab多页显示模式
ui->mdiArea->setTabsClosable(true); //页面可关闭
子窗口模式
ui->mdiArea->setViewMode(QMdiArea::SubWindowView); //子窗口模式
窗口级联展开
ui->mdiArea->cascadeSubWindows();
平铺展开
ui->mdiArea->tileSubWindows();
关闭全部子窗口
ui->mdiArea->closeAllSubWindows();