QMenuBar, QMenu and QAction
-
I've been looking at a UI which I modified in Qt Creator to add a menu, this works:
/******************************************************************************** ** Form generated from reading UI file 'splash.ui' ** ** Created by: Qt User Interface Compiler version 5.15.2 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_SPLASH_H #define UI_SPLASH_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QLabel> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenu> #include <QtWidgets/QMenuBar> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_splash { public: QAction *actionAnother_Menu; QWidget *centralwidget; QLabel *lblHdr; QLabel *lblAppTitle; QLabel *lblWidth; QLabel *lblHeight; QLabel *lblWidthVal; QLabel *lblHeightVal; QMenuBar *menuBar; QMenu *menuTest; void setupUi(QMainWindow *splash) { if (splash->objectName().isEmpty()) splash->setObjectName(QString::fromUtf8("splash")); splash->setWindowModality(Qt::NonModal); splash->resize(172, 102); splash->setFocusPolicy(Qt::NoFocus); splash->setWindowTitle(QString::fromUtf8("")); #if QT_CONFIG(tooltip) splash->setToolTip(QString::fromUtf8("")); #endif // QT_CONFIG(tooltip) #if QT_CONFIG(statustip) splash->setStatusTip(QString::fromUtf8("")); #endif // QT_CONFIG(statustip) actionAnother_Menu = new QAction(splash); actionAnother_Menu->setObjectName(QString::fromUtf8("actionAnother_Menu")); centralwidget = new QWidget(splash); centralwidget->setObjectName(QString::fromUtf8("centralwidget")); centralwidget->setStyleSheet(QString::fromUtf8("background-color:#ff333333")); lblHdr = new QLabel(centralwidget); lblHdr->setObjectName(QString::fromUtf8("lblHdr")); lblHdr->setGeometry(QRect(7, 0, 161, 38)); lblHdr->setText(QString::fromUtf8("")); lblHdr->setPixmap(QPixmap(QString::fromUtf8(":/CMPY_LOGO"))); lblHdr->setScaledContents(false); lblHdr->setAlignment(Qt::AlignCenter); lblAppTitle = new QLabel(centralwidget); lblAppTitle->setObjectName(QString::fromUtf8("lblAppTitle")); lblAppTitle->setGeometry(QRect(8, 40, 161, 20)); lblAppTitle->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblAppTitle->setAlignment(Qt::AlignCenter); lblAppTitle->setOpenExternalLinks(true); lblWidth = new QLabel(centralwidget); lblWidth->setObjectName(QString::fromUtf8("lblWidth")); lblWidth->setGeometry(QRect(8, 80, 91, 20)); lblWidth->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblWidth->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); lblHeight = new QLabel(centralwidget); lblHeight->setObjectName(QString::fromUtf8("lblHeight")); lblHeight->setGeometry(QRect(8, 60, 91, 20)); lblHeight->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblHeight->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); lblWidthVal = new QLabel(centralwidget); lblWidthVal->setObjectName(QString::fromUtf8("lblWidthVal")); lblWidthVal->setGeometry(QRect(110, 80, 60, 20)); lblWidthVal->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblWidthVal->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); lblHeightVal = new QLabel(centralwidget); lblHeightVal->setObjectName(QString::fromUtf8("lblHeightVal")); lblHeightVal->setGeometry(QRect(110, 60, 60, 20)); lblHeightVal->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblHeightVal->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); splash->setCentralWidget(centralwidget); menuBar = new QMenuBar(splash); menuBar->setObjectName(QString::fromUtf8("menuBar")); menuBar->setGeometry(QRect(0, 0, 172, 24)); menuTest = new QMenu(menuBar); menuTest->setObjectName(QString::fromUtf8("menuTest")); splash->setMenuBar(menuBar); menuBar->addAction(menuTest->menuAction()); menuTest->addAction(actionAnother_Menu); retranslateUi(splash); QMetaObject::connectSlotsByName(splash); } // setupUi void retranslateUi(QMainWindow *splash) { actionAnother_Menu->setText(QCoreApplication::translate("splash", "Another Menu", nullptr)); #if QT_CONFIG(tooltip) lblAppTitle->setToolTip(QCoreApplication::translate("splash", "XML Multi-Platform Application Manager", nullptr)); #endif // QT_CONFIG(tooltip) lblAppTitle->setText(QCoreApplication::translate("splash", "XMLMPAM", nullptr)); lblWidth->setText(QCoreApplication::translate("splash", "Display width: ", nullptr)); lblHeight->setText(QCoreApplication::translate("splash", "Display height:", nullptr)); lblWidthVal->setText(QCoreApplication::translate("splash", "0", nullptr)); lblHeightVal->setText(QCoreApplication::translate("splash", "0", nullptr)); menuTest->setTitle(QCoreApplication::translate("splash", "Test", nullptr)); (void)splash; } // retranslateUi }; namespace Ui { class splash: public Ui_splash {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_SPLASH_H
I then tried to replicate this in my own, the code is now:
} 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, "Menu bar isn't present!"); QMenu* pobjMenu(new QMenu(pobjMenuBar)); Q_ASSERT_X(pobjMenu!=nullptr, cpszClassConstr, "Menu cannot be created!"); QAction* pobjAction(pobjMenu->menuAction()); if ( pobjAction != nullptr ) { pobjAction->setShortcut(cstrShortcut); pobjAction->setText(cstrText); pobjAction->setToolTip(cstrTooltip); pobjMenuBar->addAction(pobjAction); pobjMainWindow->setMenuBar(pobjMenuBar); pobjWidget = pobjMenu; } } else if ( mstrName.compare(clsXMLnode::mscszNodeMenuBar) == 0 ) { //The parent node must be a window if ( pobjParent != nullptr && pobjParent->blnIsMainWindow() == true ) { QMainWindow* pobjMainWindow(pobjParent->pobjGetMainWindow()); if ( pobjMainWindow != nullptr ) { QMenuBar* pobjMenuBar(new QMenuBar(pobjMainWindow)); if ( pobjMenuBar != nullptr ) { int intHeight(pobjMenuBar->fontMetrics().height()); QRect rctWindow(pobjParent->rctGetGeometry()) ,rctMenuBar(0, 0, 172,24); //rctWindow.width(), intHeight); pobjMenuBar->setGeometry(rctMenuBar); pobjWidget = pobjMenuBar; } } } }
I've singled stepped in the debugger and all the pointers are correct and valid, but I do not see the menus at the top of the desktop when the window has the focus. Can anyone help or suggest how it could be different and what could be wrong?
wrote on 13 Nov 2023, 02:41 last edited by Pl45m4I used
MainWindow w; // standard mainwindow defined using UI QMainWindow* myMW(&w); // myMW points to w QMenuBar* myMenuBar(myMW->menuBar()); QMenu* myMenu(new QMenu(myMenuBar)); QAction* myAction(myMenu->menuAction()); myMenuBar->addAction(myAction); myAction->setText("myAction"); myMW->setMenuBar(myMenuBar); myMW->show(); // basically showing w
to reproduce your code.
And it works. I see myMainWindow
while the newQMenu
and the corresponding action were added to the existingQMenuBar
.I feel a bit silly to ask this, but are you aware that you are working on the "toplevel"
QMainWindow
the whole time?! Or is it just the "splash"QMainWindow
here? Somewhere (maybe even in a separate post) you've mentioned twoQMainWindow
(are you looking at the right one?!)As @SGaist mentioned, check your
QMainWindow
object tree, esp. what widget is parent of what subwidgets, menu, etc. -
I used
MainWindow w; // standard mainwindow defined using UI QMainWindow* myMW(&w); // myMW points to w QMenuBar* myMenuBar(myMW->menuBar()); QMenu* myMenu(new QMenu(myMenuBar)); QAction* myAction(myMenu->menuAction()); myMenuBar->addAction(myAction); myAction->setText("myAction"); myMW->setMenuBar(myMenuBar); myMW->show(); // basically showing w
to reproduce your code.
And it works. I see myMainWindow
while the newQMenu
and the corresponding action were added to the existingQMenuBar
.I feel a bit silly to ask this, but are you aware that you are working on the "toplevel"
QMainWindow
the whole time?! Or is it just the "splash"QMainWindow
here? Somewhere (maybe even in a separate post) you've mentioned twoQMainWindow
(are you looking at the right one?!)As @SGaist mentioned, check your
QMainWindow
object tree, esp. what widget is parent of what subwidgets, menu, etc. -
wrote on 13 Nov 2023, 08:44 last edited by
@SPlatten said in QMenuBar, QMenu and QAction:
@SGaist , how do I do that?
Do what? "dump the object tree to see how is your QMainWindow structured"?
Every
QObject
(including widgets) has const QObjectList &QObject::children() const. @SGaist suggests you write a debugging routine which calls that recursively starting from yourQMainWindow
so as to examine the actual object tree (widgets) hierarchy. -
I used
MainWindow w; // standard mainwindow defined using UI QMainWindow* myMW(&w); // myMW points to w QMenuBar* myMenuBar(myMW->menuBar()); QMenu* myMenu(new QMenu(myMenuBar)); QAction* myAction(myMenu->menuAction()); myMenuBar->addAction(myAction); myAction->setText("myAction"); myMW->setMenuBar(myMenuBar); myMW->show(); // basically showing w
to reproduce your code.
And it works. I see myMainWindow
while the newQMenu
and the corresponding action were added to the existingQMenuBar
.I feel a bit silly to ask this, but are you aware that you are working on the "toplevel"
QMainWindow
the whole time?! Or is it just the "splash"QMainWindow
here? Somewhere (maybe even in a separate post) you've mentioned twoQMainWindow
(are you looking at the right one?!)As @SGaist mentioned, check your
QMainWindow
object tree, esp. what widget is parent of what subwidgets, menu, etc. -
@SPlatten said in QMenuBar, QMenu and QAction:
@SGaist , how do I do that?
Do what? "dump the object tree to see how is your QMainWindow structured"?
Every
QObject
(including widgets) has const QObjectList &QObject::children() const. @SGaist suggests you write a debugging routine which calls that recursively starting from yourQMainWindow
so as to examine the actual object tree (widgets) hierarchy.@JonB said in QMenuBar, QMenu and QAction:
@SPlatten said in QMenuBar, QMenu and QAction:
@SGaist , how do I do that?
Do what? "dump the object tree to see how is your QMainWindow structured"?
Every
QObject
(including widgets) has const QObjectList &QObject::children() const. @SGaist suggests you write a debugging routine which calls that recursively starting from yourQMainWindow
so as to examine the actual object tree (widgets) hierarchy.I was rather thinking of dumpObjectTree but that's the idea.
-
@Pl45m4 , also, is the call to setMenuBar actually required because the menuBar came from QMainWindow in the first place ?
wrote on 13 Nov 2023, 08:48 last edited by@SPlatten
I haven't followed your code, but if you mean that it is taking the menubar which has been set on aQMainWindow
and callingsetMenuBar()
on it to set it elsewhere I don't think you can do that. But maybe that's not what your code is trying to do, I don't know. -
@JonB said in QMenuBar, QMenu and QAction:
@SPlatten said in QMenuBar, QMenu and QAction:
@SGaist , how do I do that?
Do what? "dump the object tree to see how is your QMainWindow structured"?
Every
QObject
(including widgets) has const QObjectList &QObject::children() const. @SGaist suggests you write a debugging routine which calls that recursively starting from yourQMainWindow
so as to examine the actual object tree (widgets) hierarchy.I was rather thinking of dumpObjectTree but that's the idea.
wrote on 13 Nov 2023, 08:49 last edited by@SGaist said in QMenuBar, QMenu and QAction:
I was rather thinking of dumpObjectTree but that's the idea.
LOL, I didn't notice that
QObject
already provides a method to do this for debugging. Laziness ;-) -
@JonB said in QMenuBar, QMenu and QAction:
@SPlatten said in QMenuBar, QMenu and QAction:
@SGaist , how do I do that?
Do what? "dump the object tree to see how is your QMainWindow structured"?
Every
QObject
(including widgets) has const QObjectList &QObject::children() const. @SGaist suggests you write a debugging routine which calls that recursively starting from yourQMainWindow
so as to examine the actual object tree (widgets) hierarchy.I was rather thinking of dumpObjectTree but that's the idea.
wrote on 13 Nov 2023, 09:06 last edited by@SGaist , I added:
qdbg() << "dumpObectTree"; pobjMainWindow->dumpObjectTree();
The result in my log:
dumpObectTree S000000000004E000000000214T09:04:55.954D:QMainWindow:: S000000000005E000000000214T09:04:55.954D: QMainWindowLayout::_layout S000000000006E000000000214T09:04:55.954D: QWidget:: S000000000007E000000000214T09:04:55.954D: QPropertyAnimation:: S000000000008E000000000214T09:04:55.954D: QPropertyAnimation:: S000000000009E000000000214T09:04:55.954D: QMenuBar:: S000000000010E000000000214T09:04:55.954D: QToolButton::qt_menubar_ext_button S000000000011E000000000214T09:04:55.954D: QMenu:: S000000000012E000000000214T09:04:55.954D: QAction::
-
@SGaist , I added:
qdbg() << "dumpObectTree"; pobjMainWindow->dumpObjectTree();
The result in my log:
dumpObectTree S000000000004E000000000214T09:04:55.954D:QMainWindow:: S000000000005E000000000214T09:04:55.954D: QMainWindowLayout::_layout S000000000006E000000000214T09:04:55.954D: QWidget:: S000000000007E000000000214T09:04:55.954D: QPropertyAnimation:: S000000000008E000000000214T09:04:55.954D: QPropertyAnimation:: S000000000009E000000000214T09:04:55.954D: QMenuBar:: S000000000010E000000000214T09:04:55.954D: QToolButton::qt_menubar_ext_button S000000000011E000000000214T09:04:55.954D: QMenu:: S000000000012E000000000214T09:04:55.954D: QAction::
wrote on 13 Nov 2023, 09:44 last edited by@SGaist, @JonB, @Pl45m4 , found something...
In Qt Creator I edited the splash window and added a menubar to it, I then added a single menu to that, compiled, nothing was displayed, then I added a submenu and compiled again, this time the menu was displayed. I have experimented removing the submenu, compiled and run and each time there is no submenu then the top line menu is not shown either.
-
@SGaist, @JonB, @Pl45m4 , found something...
In Qt Creator I edited the splash window and added a menubar to it, I then added a single menu to that, compiled, nothing was displayed, then I added a submenu and compiled again, this time the menu was displayed. I have experimented removing the submenu, compiled and run and each time there is no submenu then the top line menu is not shown either.
wrote on 13 Nov 2023, 09:59 last edited by JonB@SPlatten
Let's get Creator out of this. You can see the code it generates in theui_....h
file. Using that you can produce a minimal example showing the behaviour you state, for good and for bad.Depending on what that is, I have no knowledge whether MacOS is relevant, I know that does something about top-level menus. Nor do I know whether "splash screen" is relevant. I know no more than this.
-
@SPlatten
Let's get Creator out of this. You can see the code it generates in theui_....h
file. Using that you can produce a minimal example showing the behaviour you state, for good and for bad.Depending on what that is, I have no knowledge whether MacOS is relevant, I know that does something about top-level menus. Nor do I know whether "splash screen" is relevant. I know no more than this.
wrote on 13 Nov 2023, 10:28 last edited by@JonB , this is the splash ui_splash.h that works, however only when it has a submenu with actions.
/******************************************************************************** ** Form generated from reading UI file 'splash.ui' ** ** Created by: Qt User Interface Compiler version 5.15.2 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_SPLASH_H #define UI_SPLASH_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QLabel> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenu> #include <QtWidgets/QMenuBar> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_splash { public: QAction *action_New; QAction *action_Open; QAction *action_Save; QWidget *centralwidget; QLabel *lblHdr; QLabel *lblAppTitle; QLabel *lblWidth; QLabel *lblHeight; QLabel *lblWidthVal; QLabel *lblHeightVal; QMenuBar *menuBar; QMenu *menu_File; void setupUi(QMainWindow *splash) { if (splash->objectName().isEmpty()) splash->setObjectName(QString::fromUtf8("splash")); splash->setWindowModality(Qt::NonModal); splash->resize(172, 102); splash->setFocusPolicy(Qt::NoFocus); splash->setWindowTitle(QString::fromUtf8("")); #if QT_CONFIG(tooltip) splash->setToolTip(QString::fromUtf8("")); #endif // QT_CONFIG(tooltip) #if QT_CONFIG(statustip) splash->setStatusTip(QString::fromUtf8("")); #endif // QT_CONFIG(statustip) action_New = new QAction(splash); action_New->setObjectName(QString::fromUtf8("action_New")); action_Open = new QAction(splash); action_Open->setObjectName(QString::fromUtf8("action_Open")); action_Save = new QAction(splash); action_Save->setObjectName(QString::fromUtf8("action_Save")); centralwidget = new QWidget(splash); centralwidget->setObjectName(QString::fromUtf8("centralwidget")); centralwidget->setStyleSheet(QString::fromUtf8("background-color:#ff333333")); lblHdr = new QLabel(centralwidget); lblHdr->setObjectName(QString::fromUtf8("lblHdr")); lblHdr->setGeometry(QRect(7, 0, 161, 38)); lblHdr->setText(QString::fromUtf8("")); lblHdr->setPixmap(QPixmap(QString::fromUtf8(":/CMPY_LOGO"))); lblHdr->setScaledContents(false); lblHdr->setAlignment(Qt::AlignCenter); lblAppTitle = new QLabel(centralwidget); lblAppTitle->setObjectName(QString::fromUtf8("lblAppTitle")); lblAppTitle->setGeometry(QRect(8, 40, 161, 20)); lblAppTitle->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblAppTitle->setAlignment(Qt::AlignCenter); lblAppTitle->setOpenExternalLinks(true); lblWidth = new QLabel(centralwidget); lblWidth->setObjectName(QString::fromUtf8("lblWidth")); lblWidth->setGeometry(QRect(8, 80, 91, 20)); lblWidth->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblWidth->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); lblHeight = new QLabel(centralwidget); lblHeight->setObjectName(QString::fromUtf8("lblHeight")); lblHeight->setGeometry(QRect(8, 60, 91, 20)); lblHeight->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblHeight->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); lblWidthVal = new QLabel(centralwidget); lblWidthVal->setObjectName(QString::fromUtf8("lblWidthVal")); lblWidthVal->setGeometry(QRect(110, 80, 60, 20)); lblWidthVal->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblWidthVal->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); lblHeightVal = new QLabel(centralwidget); lblHeightVal->setObjectName(QString::fromUtf8("lblHeightVal")); lblHeightVal->setGeometry(QRect(110, 60, 60, 20)); lblHeightVal->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblHeightVal->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); splash->setCentralWidget(centralwidget); menuBar = new QMenuBar(splash); menuBar->setObjectName(QString::fromUtf8("menuBar")); menuBar->setGeometry(QRect(0, 0, 172, 24)); menu_File = new QMenu(menuBar); menu_File->setObjectName(QString::fromUtf8("menu_File")); splash->setMenuBar(menuBar); menuBar->addAction(menu_File->menuAction()); menu_File->addAction(action_New); menu_File->addAction(action_Open); menu_File->addAction(action_Save); retranslateUi(splash); QMetaObject::connectSlotsByName(splash); } // setupUi void retranslateUi(QMainWindow *splash) { action_New->setText(QCoreApplication::translate("splash", "&New", nullptr)); action_Open->setText(QCoreApplication::translate("splash", "&Open", nullptr)); action_Save->setText(QCoreApplication::translate("splash", "&Save", nullptr)); #if QT_CONFIG(tooltip) lblAppTitle->setToolTip(QCoreApplication::translate("splash", "XML Multi-Platform Application Manager", nullptr)); #endif // QT_CONFIG(tooltip) lblAppTitle->setText(QCoreApplication::translate("splash", "XMLMPAM", nullptr)); lblWidth->setText(QCoreApplication::translate("splash", "Display width: ", nullptr)); lblHeight->setText(QCoreApplication::translate("splash", "Display height:", nullptr)); lblWidthVal->setText(QCoreApplication::translate("splash", "0", nullptr)); lblHeightVal->setText(QCoreApplication::translate("splash", "0", nullptr)); menu_File->setTitle(QCoreApplication::translate("splash", "&File", nullptr)); (void)splash; } // retranslateUi }; namespace Ui { class splash: public Ui_splash {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_SPLASH_H
-
@JonB , this is the splash ui_splash.h that works, however only when it has a submenu with actions.
/******************************************************************************** ** Form generated from reading UI file 'splash.ui' ** ** Created by: Qt User Interface Compiler version 5.15.2 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_SPLASH_H #define UI_SPLASH_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QLabel> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenu> #include <QtWidgets/QMenuBar> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_splash { public: QAction *action_New; QAction *action_Open; QAction *action_Save; QWidget *centralwidget; QLabel *lblHdr; QLabel *lblAppTitle; QLabel *lblWidth; QLabel *lblHeight; QLabel *lblWidthVal; QLabel *lblHeightVal; QMenuBar *menuBar; QMenu *menu_File; void setupUi(QMainWindow *splash) { if (splash->objectName().isEmpty()) splash->setObjectName(QString::fromUtf8("splash")); splash->setWindowModality(Qt::NonModal); splash->resize(172, 102); splash->setFocusPolicy(Qt::NoFocus); splash->setWindowTitle(QString::fromUtf8("")); #if QT_CONFIG(tooltip) splash->setToolTip(QString::fromUtf8("")); #endif // QT_CONFIG(tooltip) #if QT_CONFIG(statustip) splash->setStatusTip(QString::fromUtf8("")); #endif // QT_CONFIG(statustip) action_New = new QAction(splash); action_New->setObjectName(QString::fromUtf8("action_New")); action_Open = new QAction(splash); action_Open->setObjectName(QString::fromUtf8("action_Open")); action_Save = new QAction(splash); action_Save->setObjectName(QString::fromUtf8("action_Save")); centralwidget = new QWidget(splash); centralwidget->setObjectName(QString::fromUtf8("centralwidget")); centralwidget->setStyleSheet(QString::fromUtf8("background-color:#ff333333")); lblHdr = new QLabel(centralwidget); lblHdr->setObjectName(QString::fromUtf8("lblHdr")); lblHdr->setGeometry(QRect(7, 0, 161, 38)); lblHdr->setText(QString::fromUtf8("")); lblHdr->setPixmap(QPixmap(QString::fromUtf8(":/CMPY_LOGO"))); lblHdr->setScaledContents(false); lblHdr->setAlignment(Qt::AlignCenter); lblAppTitle = new QLabel(centralwidget); lblAppTitle->setObjectName(QString::fromUtf8("lblAppTitle")); lblAppTitle->setGeometry(QRect(8, 40, 161, 20)); lblAppTitle->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblAppTitle->setAlignment(Qt::AlignCenter); lblAppTitle->setOpenExternalLinks(true); lblWidth = new QLabel(centralwidget); lblWidth->setObjectName(QString::fromUtf8("lblWidth")); lblWidth->setGeometry(QRect(8, 80, 91, 20)); lblWidth->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblWidth->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); lblHeight = new QLabel(centralwidget); lblHeight->setObjectName(QString::fromUtf8("lblHeight")); lblHeight->setGeometry(QRect(8, 60, 91, 20)); lblHeight->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblHeight->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); lblWidthVal = new QLabel(centralwidget); lblWidthVal->setObjectName(QString::fromUtf8("lblWidthVal")); lblWidthVal->setGeometry(QRect(110, 80, 60, 20)); lblWidthVal->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblWidthVal->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); lblHeightVal = new QLabel(centralwidget); lblHeightVal->setObjectName(QString::fromUtf8("lblHeightVal")); lblHeightVal->setGeometry(QRect(110, 60, 60, 20)); lblHeightVal->setStyleSheet(QString::fromUtf8("color:#ffffffff;")); lblHeightVal->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); splash->setCentralWidget(centralwidget); menuBar = new QMenuBar(splash); menuBar->setObjectName(QString::fromUtf8("menuBar")); menuBar->setGeometry(QRect(0, 0, 172, 24)); menu_File = new QMenu(menuBar); menu_File->setObjectName(QString::fromUtf8("menu_File")); splash->setMenuBar(menuBar); menuBar->addAction(menu_File->menuAction()); menu_File->addAction(action_New); menu_File->addAction(action_Open); menu_File->addAction(action_Save); retranslateUi(splash); QMetaObject::connectSlotsByName(splash); } // setupUi void retranslateUi(QMainWindow *splash) { action_New->setText(QCoreApplication::translate("splash", "&New", nullptr)); action_Open->setText(QCoreApplication::translate("splash", "&Open", nullptr)); action_Save->setText(QCoreApplication::translate("splash", "&Save", nullptr)); #if QT_CONFIG(tooltip) lblAppTitle->setToolTip(QCoreApplication::translate("splash", "XML Multi-Platform Application Manager", nullptr)); #endif // QT_CONFIG(tooltip) lblAppTitle->setText(QCoreApplication::translate("splash", "XMLMPAM", nullptr)); lblWidth->setText(QCoreApplication::translate("splash", "Display width: ", nullptr)); lblHeight->setText(QCoreApplication::translate("splash", "Display height:", nullptr)); lblWidthVal->setText(QCoreApplication::translate("splash", "0", nullptr)); lblHeightVal->setText(QCoreApplication::translate("splash", "0", nullptr)); menu_File->setTitle(QCoreApplication::translate("splash", "&File", nullptr)); (void)splash; } // retranslateUi }; namespace Ui { class splash: public Ui_splash {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_SPLASH_H
wrote on 13 Nov 2023, 11:11 last edited byFinally 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! }
-
@Pl45m4 , what do you mean, "are you aware that you are working on the "toplevel" QMainWindow the whole time" ?
wrote on 13 Nov 2023, 14:34 last edited by@SPlatten said in QMenuBar, QMenu and QAction:
@Pl45m4 , what do you mean, "are you aware that you are working on the "toplevel" QMainWindow the whole time" ?
Are there two MainWindows involved in your app or is it just one?! Does this MainWindow have a
QMenuBar
already? Because your arent creating a new one -
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! }
wrote on 13 Nov 2023, 16:23 last edited by Pl45m4@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 13 Nov 2023, 22:22
16/21