Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMenuBar, QMenu and QAction
Forum Updated to NodeBB v4.3 + New Features

QMenuBar, QMenu and QAction

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 2.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I am trying to programmatically add QMenuBar, QMenu and QAction to a dynamically created instance of QMainWindow.

    The configuration is read from an XML file:

    <menubar id="mnuBar">
       <menu id="mnuFile" text="File" tooltip="File tooltip!" shortcut="Ctrl+F">
          <action text="New" tooltip="New tooltip!" shortcut="Ctrl+N"/>
          <action text="Open" tooltip="Open tooltip!" shortcut="Ctrl+O"/>
          <action text="Save" tooltip="Save tooltip!" shortcut="Ctrl+S"/>
        </menu>
    </menubar>
    

    The code that processes the XML:

            } 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));
                QMenuBar* pobjMenuBar(qobject_cast<QMenuBar*>(pobjParWidget));
                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);
                    pobjWidget = pobjMenu;
                }
            } else if ( mstrName.compare(clsXMLnode::mscszNodeMenuBar) == 0 ) {
        //The parent node must be a window
                if ( pobjParent != nullptr ) {
                    QMainWindow* pobjWindow(pobjParent->pobjGetMainWindow());
                    if ( pobjWindow != nullptr ) {
                        QMenuBar* pobjMenuBar(new QMenuBar(pobjWindow));
                        if ( pobjMenuBar != nullptr ) {
                            int intHeight(pobjMenuBar->fontMetrics().height());
                            QRect rctWindow(pobjWindow->geometry())
                                 ,rctMenuBar(0, 0, rctWindow.width(), intHeight);
                            pobjMenuBar->setGeometry(rctMenuBar);
                            pobjWindow->setMenuBar(pobjMenuBar);
                            pobjWidget = pobjMenuBar;
                        }
                    }
                }
            }
    

    I've been through this quite a few times and everything in the debugger looks ok, but I'm not seeing the menu bar or the menu in the window. Everything else looks ok, I've compared the logic with a UI I've created in Qt Creator and cannot see what I've missed.

    Kind Regards,
    Sy

    SGaistS 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I am trying to programmatically add QMenuBar, QMenu and QAction to a dynamically created instance of QMainWindow.

      The configuration is read from an XML file:

      <menubar id="mnuBar">
         <menu id="mnuFile" text="File" tooltip="File tooltip!" shortcut="Ctrl+F">
            <action text="New" tooltip="New tooltip!" shortcut="Ctrl+N"/>
            <action text="Open" tooltip="Open tooltip!" shortcut="Ctrl+O"/>
            <action text="Save" tooltip="Save tooltip!" shortcut="Ctrl+S"/>
          </menu>
      </menubar>
      

      The code that processes the XML:

              } 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));
                  QMenuBar* pobjMenuBar(qobject_cast<QMenuBar*>(pobjParWidget));
                  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);
                      pobjWidget = pobjMenu;
                  }
              } else if ( mstrName.compare(clsXMLnode::mscszNodeMenuBar) == 0 ) {
          //The parent node must be a window
                  if ( pobjParent != nullptr ) {
                      QMainWindow* pobjWindow(pobjParent->pobjGetMainWindow());
                      if ( pobjWindow != nullptr ) {
                          QMenuBar* pobjMenuBar(new QMenuBar(pobjWindow));
                          if ( pobjMenuBar != nullptr ) {
                              int intHeight(pobjMenuBar->fontMetrics().height());
                              QRect rctWindow(pobjWindow->geometry())
                                   ,rctMenuBar(0, 0, rctWindow.width(), intHeight);
                              pobjMenuBar->setGeometry(rctMenuBar);
                              pobjWindow->setMenuBar(pobjMenuBar);
                              pobjWidget = pobjMenuBar;
                          }
                      }
                  }
              }
      

      I've been through this quite a few times and everything in the debugger looks ok, but I'm not seeing the menu bar or the menu in the window. Everything else looks ok, I've compared the logic with a UI I've created in Qt Creator and cannot see what I've missed.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      On which OS are you running that ?
      If Linux, which desktop environment are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      SPlattenS M 3 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        On which OS are you running that ?
        If Linux, which desktop environment are you using ?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @SGaist , macOS Monterey, Version 12.7.1, I know what your thinking I think :)

        The menu isn't appearing not the desktop menu either.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          On which OS are you running that ?
          If Linux, which desktop environment are you using ?

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          @SGaist , I just used Qt Creator to add a menu to the splash window I've created and when the splash window gets the focus, the menu does appear in the top left of the desktop, but this isn't true of my programmatically created window when it gets the focus.

          Kind Regards,
          Sy

          SPlattenS 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @SGaist , I just used Qt Creator to add a menu to the splash window I've created and when the splash window gets the focus, the menu does appear in the top left of the desktop, but this isn't true of my programmatically created window when it gets the focus.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            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?

            Kind Regards,
            Sy

            SGaistS Pl45m4P 2 Replies Last reply
            0
            • SPlattenS SPlatten

              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?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Did you try to dump the object tree to see how is your QMainWindow structured ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              SPlattenS 1 Reply Last reply
              0
              • SPlattenS SPlatten

                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?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @SPlatten

                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 my MainWindow while the new QMenu and the corresponding action were added to the existing QMenuBar.

                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 two QMainWindow (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.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                SPlattenS 2 Replies Last reply
                0
                • SGaistS SGaist

                  Did you try to dump the object tree to see how is your QMainWindow structured ?

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #8

                  @SGaist , how do I do that?

                  Kind Regards,
                  Sy

                  JonBJ 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @SPlatten

                    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 my MainWindow while the new QMenu and the corresponding action were added to the existing QMenuBar.

                    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 two QMainWindow (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.

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @Pl45m4 , what do you mean, "are you aware that you are working on the "toplevel" QMainWindow the whole time" ?

                    Kind Regards,
                    Sy

                    Pl45m4P 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @SGaist , how do I do that?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @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 your QMainWindow so as to examine the actual object tree (widgets) hierarchy.

                      SGaistS 1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4

                        @SPlatten

                        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 my MainWindow while the new QMenu and the corresponding action were added to the existing QMenuBar.

                        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 two QMainWindow (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.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #11

                        @Pl45m4 , also, is the call to setMenuBar actually required because the menuBar came from QMainWindow in the first place ?

                        Kind Regards,
                        Sy

                        JonBJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @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 your QMainWindow so as to examine the actual object tree (widgets) hierarchy.

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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 your QMainWindow so as to examine the actual object tree (widgets) hierarchy.

                          I was rather thinking of dumpObjectTree but that's the idea.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          JonBJ SPlattenS 2 Replies Last reply
                          1
                          • SPlattenS SPlatten

                            @Pl45m4 , also, is the call to setMenuBar actually required because the menuBar came from QMainWindow in the first place ?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @SPlatten
                            I haven't followed your code, but if you mean that it is taking the menubar which has been set on a QMainWindow and calling setMenuBar() 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.

                            1 Reply Last reply
                            0
                            • SGaistS SGaist

                              @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 your QMainWindow so as to examine the actual object tree (widgets) hierarchy.

                              I was rather thinking of dumpObjectTree but that's the idea.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @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 ;-)

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @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 your QMainWindow so as to examine the actual object tree (widgets) hierarchy.

                                I was rather thinking of dumpObjectTree but that's the idea.

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #15

                                @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::
                                

                                Kind Regards,
                                Sy

                                SPlattenS 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @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::
                                  
                                  SPlattenS Offline
                                  SPlattenS Offline
                                  SPlatten
                                  wrote on last edited by
                                  #16

                                  @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.

                                  Kind Regards,
                                  Sy

                                  JonBJ 1 Reply Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @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.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #17

                                    @SPlatten
                                    Let's get Creator out of this. You can see the code it generates in the ui_....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.

                                    SPlattenS 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @SPlatten
                                      Let's get Creator out of this. You can see the code it generates in the ui_....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.

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #18

                                      @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
                                      

                                      Kind Regards,
                                      Sy

                                      SPlattenS 1 Reply Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @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
                                        
                                        SPlattenS Offline
                                        SPlattenS Offline
                                        SPlatten
                                        wrote on last edited by
                                        #19

                                        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!
                                              }
                                        

                                        Kind Regards,
                                        Sy

                                        Pl45m4P 1 Reply Last reply
                                        1
                                        • SPlattenS SPlatten

                                          @Pl45m4 , what do you mean, "are you aware that you are working on the "toplevel" QMainWindow the whole time" ?

                                          Pl45m4P Offline
                                          Pl45m4P Offline
                                          Pl45m4
                                          wrote on last edited by
                                          #20

                                          @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


                                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                          ~E. W. Dijkstra

                                          1 Reply Last reply
                                          1

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved