PDA

View Full Version : How to show SubMenu next to MainMenu?



alex chpenst
16th July 2008, 18:17
Hi,

I've added a submenu to the main menu with the following command:

subMenu = mainMenu->addMenu("Recent files");
subMenu->addAction(....);
...

The problem is that when I run my application and open subMenu, it's not shown right next to the mainMenu. It might have different positions on the screen and might be not attached to the mainMenu at all.

How to bind the subMenu to the mainMenu?

Thanks for any help!!

aamer4yu
16th July 2008, 19:22
Can u provide some snapshot of how u want it to appear ?
Also what have u tried ??

alex chpenst
17th July 2008, 08:37
The first attached picture is what I have. On the second picture is what I want to have (like it's normally). The first situation happens always when the submenu is shown first time (either when the application has just started or when the content of the submenu has changed). When the same content of the submenu has already been shown once then it's always like on the second picture (but still not perfectly aligned!!).

(The main menu is a contex menu shown when one of the tray icons is triggered. The submenu on the left is shown when action "Change active profile" is triggered).

aamer4yu
17th July 2008, 10:33
Can u post the code how you are doing it ?

alex chpenst
17th July 2008, 11:33
The code is below. Some things are missing, but I hope it's clear.. Thanks!


void Tray::createMenu()
{
trayIconMenu = new QMenu(this);
fileMenu = trayIconMenu->addMenu("Change active profile");
connect(fileMenu, SIGNAL(aboutToShow()), this, SLOT(activateFileMenu()));

trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}

void Tray::activateFileMenu()
{
fileMenu->clear();
QList<QString> allfiles = files->getAllFiles(); // retrieve file names
fileGroupAction = new QActionGroup(this);
QAction *fileAction;
for (int i = 0; i < allfiles.size(); ++i) { // create a certain number of actions in fileMenu
fileAction = new QAction (files->getIcon(allfiles.at(i)), allfiles.at(i), fileGroupAction);
fileAction->setCheckable(true);
if (files->getImportance(allfiles.at(i))==1)
fileAction->setChecked(true); // one of the files is checked by default
fileMenu->addAction(fileAction);
fileGroupAction->addAction(fileAction);
connect(fileAction, SIGNAL(triggered()), this, SLOT(changeActiveFile()));
}
}