QAction, setting menu breaks / stops triggered signal
-
Working on clients system, older version of Qt 4.8.4. Programmatically I'm creating the menu structure:
void clsMainWnd::addMenu(const QStringList& crslstMenu) { if ( crslstMenu.length() < MI_MIN_MENU_OPTIONS ) { return; } if ( mpobjMenuBar == nullptr ) { mpobjMenuBar = menuBar(); } QMenu* pobjMenu(nullptr); if ( crslstMenu.length() > MI_PARENT_MENU_ID ) { tMenusMap::Iterator itFound(mmpMenus .find(crslstMenu[MI_PARENT_MENU_ID])); if ( itFound != mmpMenus.end() && (pobjMenu = itFound.value()) != nullptr ) { QAction* pobjAction(pobjMenu->addAction(crslstMenu[MI_MENU_TEXT])); if ( pobjAction != nullptr ) { pobjAction->setMenu(pobjMenu); pobjAction->setObjectName(QString(crslstMenu[MI_MENU_TEXT])); QObject::connect(pobjMenu, SIGNAL(triggered(QAction*)) ,this, SLOT(onMenuSelected(QAction*))); QObject::connect(pobjAction, SIGNAL(triggered)) ,this, SLOT(onMenuSelected())); } } } else if ( (pobjMenu = new QMenu(crslstMenu[MI_MENU_TEXT] ,mpobjMenuBar)) != nullptr ) { mpobjMenuBar->addMenu(pobjMenu); pobjMenu->setObjectName(QString[crslstMenu[MI_MENU_ID])); mmpMenus.insert(crslstMenu[MI_MENU_ID], pobjMenu); } }
The issue is when the line:
pobjAction->setMenu(pobjMenu);
is included in the code, the triggered signals do not get emitted....why?
[Edit] I've removed the setMenu as it just doesn't do anything useful except break the code. Instead I've used setProperty and property in the slot to identify the menu action.
-
Working on clients system, older version of Qt 4.8.4. Programmatically I'm creating the menu structure:
void clsMainWnd::addMenu(const QStringList& crslstMenu) { if ( crslstMenu.length() < MI_MIN_MENU_OPTIONS ) { return; } if ( mpobjMenuBar == nullptr ) { mpobjMenuBar = menuBar(); } QMenu* pobjMenu(nullptr); if ( crslstMenu.length() > MI_PARENT_MENU_ID ) { tMenusMap::Iterator itFound(mmpMenus .find(crslstMenu[MI_PARENT_MENU_ID])); if ( itFound != mmpMenus.end() && (pobjMenu = itFound.value()) != nullptr ) { QAction* pobjAction(pobjMenu->addAction(crslstMenu[MI_MENU_TEXT])); if ( pobjAction != nullptr ) { pobjAction->setMenu(pobjMenu); pobjAction->setObjectName(QString(crslstMenu[MI_MENU_TEXT])); QObject::connect(pobjMenu, SIGNAL(triggered(QAction*)) ,this, SLOT(onMenuSelected(QAction*))); QObject::connect(pobjAction, SIGNAL(triggered)) ,this, SLOT(onMenuSelected())); } } } else if ( (pobjMenu = new QMenu(crslstMenu[MI_MENU_TEXT] ,mpobjMenuBar)) != nullptr ) { mpobjMenuBar->addMenu(pobjMenu); pobjMenu->setObjectName(QString[crslstMenu[MI_MENU_ID])); mmpMenus.insert(crslstMenu[MI_MENU_ID], pobjMenu); } }
The issue is when the line:
pobjAction->setMenu(pobjMenu);
is included in the code, the triggered signals do not get emitted....why?
[Edit] I've removed the setMenu as it just doesn't do anything useful except break the code. Instead I've used setProperty and property in the slot to identify the menu action.
@SPlatten said in QAction, setting menu breaks / stops triggered signal:
The issue is when the line:
pobjAction->setMenu(pobjMenu);is included in the code, the triggered signals do not get emitted....why?
Why?! Expected behavior, I would say...
If you turn your
QAction
into a menu (by callingsetMenu
), you can not "trigger" it anymore, but you open the submenu, when you click it.
I think, you simply lose the "trigger" functionality by turning your action into a menu action.Edit:
If you want to react on the click anyway, you could try the
aboutToShow()
signal. This doesn't work for contextMenus since they pop-up on hover and don't need a click, but works inQMenuBars
.