PDA

View Full Version : problem with menu entries for mdi windows



y.shan
17th September 2007, 16:15
Hi all!

I hope this wasn't discussed before, as I haven't found any related posts...

so my problem is that, i want to make a submenu, where i can list all my currently active mdi windows like in the microsoft office programs, where you can switch between the documents.

in qt 3, that wasn't a problem since there was a insertItem function, which didn't need a QAction.

in qt 4 you are only able to add a menu entry via a QAction, which is quite disturbing me.
i have already looked at the recent files example, which is similar to my problem with the difference, that in this example you assume that there is a maximum count of available entries. (you defined a enum { MAX_RECENTFILES = 9} for example and then defined QAction *recentFiles[MAX_RECENTFILES] in your header file to get the QActions, which I can't do since in the header file I don't know how many active windows there are gonna be)

BTW: I'm quite new, so chance is high that i missed a very easy way of doing this.

marcel
17th September 2007, 17:39
Nut if you don't want a QAction for the menu entry, how will you know when the user clicks one of the entries?

You have QMenu::addAction(const QString&). You can use its result(the QAction) and connect to its triggered signal. That's when the user clicked your menu entry and you can activate the corresponding window.

You also have variations on that: addMenu(const QString&), etc

spud
17th September 2007, 17:53
You must have missed the MDI example.



// during construction:
connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu()));

void MainWindow::updateWindowMenu()
{
windowMenu->clear();
windowMenu->addAction(closeAct);
windowMenu->addAction(closeAllAct);
windowMenu->addSeparator();
windowMenu->addAction(tileAct);
windowMenu->addAction(cascadeAct);
windowMenu->addSeparator();
windowMenu->addAction(nextAct);
windowMenu->addAction(previousAct);
windowMenu->addAction(separatorAct);

QList<QMdiSubWindow *> windows = mdiArea->subWindowList();
separatorAct->setVisible(!windows.isEmpty());

for (int i = 0; i < windows.size(); ++i) {
MdiChild *child = qobject_cast<MdiChild *>(windows.at(i)->widget());

QString text;
if (i < 9) {
text = tr("&%1 %2").arg(i + 1)
.arg(child->userFriendlyCurrentFile());
} else {
text = tr("%1 %2").arg(i + 1)
.arg(child->userFriendlyCurrentFile());
}
QAction *action = windowMenu->addAction(text);
action->setCheckable(true);
action ->setChecked(child == activeMdiChild());
connect(action, SIGNAL(triggered()), windowMapper, SLOT(map()));
windowMapper->setMapping(action, windows.at(i));
}
}

y.shan
18th September 2007, 07:48
You must have missed the MDI example.


indeed i missed the example. Though i looked at it, i only looked at the windows menu and couldn't find a windows submenu, hence i thought that wasn't the right example. Now i noticed that the windows are appended to the menu.

thx for your help