QMenuBar, QMenu and QAction
-
Finally fixed:
if ( mstrName.compare(clsXMLnode::mscszNodeAction) == 0 ) { Q_ASSERT_X(blnIsParentAMenu()==true, cpszClassConstr , "Actions only be used in a menu!"); Q_ASSERT_X(pobjParWidget!=nullptr, cpszClassConstr, "No menubar!"); const QString cstrShortcut(strGetAttribute(clsXMLnode::mscszAttrShortcut)) ,cstrText(strGetAttribute(clsXMLnode::mscszAttrText)) ,cstrTooltip(strGetAttribute(clsXMLnode::mscszAttrTooltip)); QMenu* pobjParentMenu(qobject_cast<QMenu*>(pobjParWidget)); Q_ASSERT_X(pobjParentMenu!=nullptr, cpszClassConstr, "Widget is not a menu!"); QWidget* pobjWindow(pobjParentMenu->window()); Q_ASSERT_X(pobjWindow!=nullptr, cpszClassConstr, "Cannot get window!"); QAction* pobjAction(new QAction(pobjWindow)); Q_ASSERT_X(pobjAction!=nullptr, cpszClassConstr, "Cannot create an action!"); pobjAction->setShortcut(QCoreApplication::translate("clsMainWnd" ,cstrShortcut.toLatin1() ,nullptr)); pobjAction->setText(cstrText); pobjAction->setToolTip(cstrTooltip); pobjParentMenu->addAction(pobjAction); } else if ( mstrName.compare(clsXMLnode::mscszNodeMenu) == 0 ) { Q_ASSERT_X(blnIsParentAMenuBar()==true, cpszClassConstr , "Menu's can only be added to a menu bar!"); const QString cstrShortcut(strGetAttribute(clsXMLnode::mscszAttrShortcut)) ,cstrText(strGetAttribute(clsXMLnode::mscszAttrText)) ,cstrTooltip(strGetAttribute(clsXMLnode::mscszAttrTooltip)); clsXMLnode* pobjMainWindowNode(nullptr); if ( pobjParent != nullptr && pobjParent->mpobjParent != nullptr ) { pobjMainWindowNode = pobjParent->mpobjParent; } Q_ASSERT_X(pobjMainWindowNode!=nullptr && pobjMainWindowNode->mpobjMainWindow!=nullptr, cpszClassConstr ,"Cannot get parent window!"); QMainWindow* pobjMainWindow(pobjMainWindowNode->mpobjMainWindow); QMenuBar* pobjMenuBar(pobjMainWindow->menuBar()); Q_ASSERT_X(pobjMenuBar!=nullptr, cpszClassConstr, "Cannot get menu bar!"); QMenu* pobjMenu(new QMenu(pobjMenuBar)); Q_ASSERT_X(pobjMenu!=nullptr, cpszClassConstr, "Cannot create menu!"); QAction* pobjAction(pobjMenu->menuAction()); Q_ASSERT_X(pobjAction!=nullptr, cpszClassConstr, "Cannot get action!"); pobjAction->setShortcut(cstrShortcut); pobjAction->setText(cstrText); pobjAction->setToolTip(cstrTooltip); pobjMenuBar->addAction(pobjAction); pobjMenu->show(); pobjMenuBar->show(); pobjWidget = pobjMenu; } else if ( mstrName.compare(clsXMLnode::mscszNodeMenuBar) == 0 ) { //Nothing required menubar should already be present in an instance of QMainWindow! }
@SPlatten said in QMenuBar, QMenu and QAction:
pobjMenu->show();
pobjMenuBar->show();Still weird that this seems to fix it because usually you dont have to show a
QMenuBar
explicitly.
Or what fixed it in the end?QMainWindow::menuBar()
says:Returns the menu bar for the main window. This function creates and returns an empty menu bar if the menu bar does not exist.
And this, either existing
QMenuBar
or the newly created one, is already a child ofQMainWindow
.
So I wouldn't expect that you have to show it or apply it to yourmainWindow
manually again.Dont know if this is somewhat relevant for you but reading the documentation further, it says:
If you want all windows in a Mac application to share one menu bar, don't use this function to create it, because the menu bar created here will have this QMainWindow as its parent. Instead, you must create a menu bar that does not have a parent, which you can then share among all the Mac windows. Create a parent-less menu bar this way:
QMenuBar *menuBar = new QMenuBar(0);
-
J JKSH forked this topic on