[SOLVED] QPushbutton with QMenu - menu does not go away until selected twice
-
Is this a bug, or am I doing something wrong? When I put a QMenu in a QPushButton, and the button is pushed, a menu appears. If I select a menu item, the action is triggered, but the menu stays displayed. If I select the same action again, or a different action, then the menu disappears. I want the menu to disappear every time I select an action.
@void RegisterBrowser::initializeMenus()
{ readMenu = new QMenu("Read Menu", mainWindow);
readAllAction = new QAction("Read All", mainWindow);
readOneAction = new QAction("Read One", mainWindow);
readMenu->addAction(readAllAction);
readMenu->addAction(readOneAction);
ui->ReadRegisterButton->setMenu(readMenu);
connect(readAllAction, SIGNAL(triggered()), this, SLOT(readAllRegistersButtonPressed()));
connect(readOneAction, SIGNAL(triggered()), this, SLOT(readOneRegisterButtonPressed()));
}
void RegisterBrowser::readRegisterButtonPressed()
{
ui->ReadRegisterButton->showMenu();
}@
It works perfectly if I don't put the QMenu in the QPushButton, and handle it manually like this:
@ QPoint globalPos = ui->ReadRegisterButton->mapToGlobal(QPoint(0,ui->ReadRegisterButton->height()));
readMenu->exec(globalPos);@ -
Remove the code below from 'readRegisterButtonPressed()'.
Qt will show the menu automatically..[code]
void RegisterBrowser::readRegisterButtonPressed()
{
ui->ReadRegisterButton->showMenu();
}
[/code] -
Thanks! It worked perfectly.