How in Qt can I implement the Window menu of an MDI application that reflects open internal windows and allows switching between them? While it comes to mind only adding a QAction when opening the inner window and deleting this item when closing, but somehow it is hemorrhoid. Are there any ready-made solutions in Qt?
|
2 answers
I implemented so
GasSolver::GasSolver (QWidget *parent) : QMainWindow (parent), ui (new Ui::GasSolver) { ui->setupUi (this); connect (ui->mWindows, SIGNAL (triggered (QAction *)), this, SLOT (menu_win_triggered (QAction *)));//выбор окна connect (ui->mWindows, SIGNAL (aboutToShow()), this, SLOT (update_winlist())); } void GasSolver::menu_win_triggered (QAction *a) { int pos = ui->mWindows->actions().indexOf (a); if (pos == -1) { } else if (pos == 0) { ui->mdiArea->cascadeSubWindows(); } else if (pos == 1) { ui->mdiArea->tileSubWindows(); } else { pos -= 3; ui->mdiArea->setActiveSubWindow (ui->mdiArea->subWindowList().at (pos));//показать окно } } void GasSolver::update_winlist() { while (ui->mWindows->actions().count() > 3) { ui->mWindows->removeAction (ui->mWindows->actions().at (3)); } QList<QMdiSubWindow *> wlist = ui->mdiArea->subWindowList(); for (qint64 i = 0; i < wlist.size(); i++) { ui->mWindows->addAction (QString ("%1. %2").arg (i + 1).arg (wlist.at (i)->windowTitle())); } if (ui->mdiArea->subWindowList().empty()) { ui->WinCascade->setEnabled (false); ui->WinMozaik->setEnabled (false); } else { ui->WinCascade->setEnabled (true); ui->WinMozaik->setEnabled (true); } } Initially, the window menu has items cascaded and tiled.
- Thank. I'll try. - Artik
|
In general, I came to this method (thanks to Evgeny Shmidt for the tip):
void MainWindow::updateWindowsMenu() { while(mnWindows->actions().count()>3) mnWindows->removeAction(mnWindows->actions().at(3)); QList<QMdiSubWindow*> list = mdiArea->subWindowList(); for(int i=0; i<list.size(); i++){ mnWindows->addAction(list[i]->windowTitle()); mnWindows->actions().at(i+3)->setCheckable(true); mnWindows->actions().at(i+3)->setStatusTip("Открыть "+list[i]->windowTitle()); if(list[i]==mdiArea->activeSubWindow()) mnWindows->actions().at(i+3)->setChecked(true); connect(mnWindows->actions().at(i+3), SIGNAL(triggered(bool)), list[i], SLOT(setFocus())); } } "3", because The first three menu items ( cascadeSubWindows() , closeAllSubWindows() and addSeparator() ) are immutable. We make the method a slot and subWindowActivated(QMdiSubWindow*) to the subWindowActivated(QMdiSubWindow*) signal subWindowActivated(QMdiSubWindow*) QMdiArea widget QMdiArea .
|