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. Menus not showing up in MainWindow

Menus not showing up in MainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 710 Views
  • 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I have the feeling that im blind here.

    I tryed to create Menus for my MainWindow. However they don't show up. I can't find the issue why.

    The code to make the menus:

    private:
        void createActions();
        void createMenus();
    
        Game *mGame;
    
        QMenu *mGameMenu;
        QMenu *mHelpMenu;
    
        // Game Menu
        QAction *mNewAction;
        QAction *mBeginnerAction;
        QAction *mIntermediateAction;
        QAction *mExpertAction;
        QAction *mCustomAction;
        QAction *mMarksAction;
        QAction *mColorAction;
        QAction *mBestTimes;
        QAction *mExit;
    
        // Help Menu
        QAction *mAbout;
        QAction *mAboutQtAction;
    };
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        mGame = new Game;
    
        setCentralWidget(mGame);
    
        void createActions();
        void createMenus();
    }
    
    void MainWindow::createActions()
    {
        mNewAction = new QAction{tr("&New"), this};
        mNewAction->setShortcut(Qt::Key::Key_F2);
        connect(mNewAction, &QAction::triggered,
                this, &MainWindow::newGame);
    
        //... And so on....
    }
    
    void MainWindow::createMenus()
    {
        mGameMenu = menuBar()->addMenu(tr("&Game"));
        mGameMenu->addAction(mNewAction);
        mGameMenu->addSeparator();
        mGameMenu->addAction(mBeginnerAction);
        mGameMenu->addAction(mIntermediateAction);
        mGameMenu->addAction(mExpertAction);
        mGameMenu->addAction(mCustomAction);
        mGameMenu->addSeparator();
        mGameMenu->addAction(mMarksAction);
        mGameMenu->addAction(mColorAction);
        mGameMenu->addSeparator();
        mGameMenu->addAction(mBestTimes);
        mGameMenu->addAction(mExit);
    
        mHelpMenu = menuBar()->addMenu(tr("&Help"));
        mHelpMenu->addAction(mAbout);
        mHelpMenu->addAction(mAboutQtAction);
    }
    
    JonBJ 1 Reply Last reply
    0
    • S sandro4912

      I have the feeling that im blind here.

      I tryed to create Menus for my MainWindow. However they don't show up. I can't find the issue why.

      The code to make the menus:

      private:
          void createActions();
          void createMenus();
      
          Game *mGame;
      
          QMenu *mGameMenu;
          QMenu *mHelpMenu;
      
          // Game Menu
          QAction *mNewAction;
          QAction *mBeginnerAction;
          QAction *mIntermediateAction;
          QAction *mExpertAction;
          QAction *mCustomAction;
          QAction *mMarksAction;
          QAction *mColorAction;
          QAction *mBestTimes;
          QAction *mExit;
      
          // Help Menu
          QAction *mAbout;
          QAction *mAboutQtAction;
      };
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          mGame = new Game;
      
          setCentralWidget(mGame);
      
          void createActions();
          void createMenus();
      }
      
      void MainWindow::createActions()
      {
          mNewAction = new QAction{tr("&New"), this};
          mNewAction->setShortcut(Qt::Key::Key_F2);
          connect(mNewAction, &QAction::triggered,
                  this, &MainWindow::newGame);
      
          //... And so on....
      }
      
      void MainWindow::createMenus()
      {
          mGameMenu = menuBar()->addMenu(tr("&Game"));
          mGameMenu->addAction(mNewAction);
          mGameMenu->addSeparator();
          mGameMenu->addAction(mBeginnerAction);
          mGameMenu->addAction(mIntermediateAction);
          mGameMenu->addAction(mExpertAction);
          mGameMenu->addAction(mCustomAction);
          mGameMenu->addSeparator();
          mGameMenu->addAction(mMarksAction);
          mGameMenu->addAction(mColorAction);
          mGameMenu->addSeparator();
          mGameMenu->addAction(mBestTimes);
          mGameMenu->addAction(mExit);
      
          mHelpMenu = menuBar()->addMenu(tr("&Help"));
          mHelpMenu->addAction(mAbout);
          mHelpMenu->addAction(mAboutQtAction);
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @sandro4912 said in Menus not showing up in MainWindow:

      mNewAction = new QAction{tr("&New"), this};

      Are you really using { ... }, not ( ... ), there? @Christian-Ehrlicher has explained below that this is yet another C++ initialization syntax....

      Break your problem down: start out with just the two menuBar()->addMenu()s, no QActions, and verify the menu shows up? Is it your top-level menus on the menubar or their action items which do not appear?

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @sandro4912 said in Menus not showing up in MainWindow:

        {
        mGame = new Game;

        setCentralWidget(mGame);
        
        void createActions();
        void createMenus();
        

        }

        Why do you define 2 local functions here?

        @JonB

        Are you really using { ... }, not ( ... ), there?

        It's allowed since c++11 - beautiful nice 'uniform initialization'... :/

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        JonBJ 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @sandro4912 said in Menus not showing up in MainWindow:

          {
          mGame = new Game;

          setCentralWidget(mGame);
          
          void createActions();
          void createMenus();
          

          }

          Why do you define 2 local functions here?

          @JonB

          Are you really using { ... }, not ( ... ), there?

          It's allowed since c++11 - beautiful nice 'uniform initialization'... :/

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

          @Christian-Ehrlicher said in Menus not showing up in MainWindow:

          @sandro4912 said in Menus not showing up in MainWindow:

          {
          mGame = new Game;

          setCentralWidget(mGame);
          
          void createActions();
          void createMenus();
          

          }

          Why do you define 2 local functions here?

          @sandrobrito I had not noticed this. @Christian-Ehrlicher is right! What you have written does not call your createActions/Menus() methods at all. Remove the void from the front of these and your menus will appear :)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by
            #5

            one of these mistake you look at and don't see because they are very stupid. removing the void solved the issue as expected

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sandro4912
              wrote on last edited by sandro4912
              #6

              I just want to add the posted code also crashes with a segmentation fault int this line;

              mGameMenu->addAction(mCustomAction);
              

              reason is this:

                  mExpertAction = new QAction{tr("&Expert"), this};
                  mExpertAction->setCheckable(true);
                  connect(mExpertAction, &QAction::triggered,
                          this, &MainWindow::expertGame);
              
                  mExpertAction = new QAction{tr("&Custom..."), this};
                  mExpertAction->setCheckable(true);
                  connect(mExpertAction, &QAction::triggered,
                          this, &MainWindow::customGame);
              

              The second should be mCustomAction

              And also this:

              mColorAction = new QAction{tr("C&olor"), this};
                mColorAction->setCheckable(true);
                mColorAction->setChecked(true);
                connect(mColorAction, &QAction::triggered,
                        this, &MainWindow::toggleColor);
              
                mColorAction = new QAction{tr("&Best Times..."), this};
                connect(mColorAction, &QAction::triggered,
                        this, &MainWindow::showBestTimes);
              

              The second should be mBestTimesAction

              jsulmJ JonBJ 2 Replies Last reply
              0
              • S sandro4912

                I just want to add the posted code also crashes with a segmentation fault int this line;

                mGameMenu->addAction(mCustomAction);
                

                reason is this:

                    mExpertAction = new QAction{tr("&Expert"), this};
                    mExpertAction->setCheckable(true);
                    connect(mExpertAction, &QAction::triggered,
                            this, &MainWindow::expertGame);
                
                    mExpertAction = new QAction{tr("&Custom..."), this};
                    mExpertAction->setCheckable(true);
                    connect(mExpertAction, &QAction::triggered,
                            this, &MainWindow::customGame);
                

                The second should be mCustomAction

                And also this:

                mColorAction = new QAction{tr("C&olor"), this};
                  mColorAction->setCheckable(true);
                  mColorAction->setChecked(true);
                  connect(mColorAction, &QAction::triggered,
                          this, &MainWindow::toggleColor);
                
                  mColorAction = new QAction{tr("&Best Times..."), this};
                  connect(mColorAction, &QAction::triggered,
                          this, &MainWindow::showBestTimes);
                

                The second should be mBestTimesAction

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @sandro4912 said in Menus not showing up in MainWindow:

                mGameMenu

                Did you assign a valid pointer to mGameMenu before using it?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S sandro4912

                  I just want to add the posted code also crashes with a segmentation fault int this line;

                  mGameMenu->addAction(mCustomAction);
                  

                  reason is this:

                      mExpertAction = new QAction{tr("&Expert"), this};
                      mExpertAction->setCheckable(true);
                      connect(mExpertAction, &QAction::triggered,
                              this, &MainWindow::expertGame);
                  
                      mExpertAction = new QAction{tr("&Custom..."), this};
                      mExpertAction->setCheckable(true);
                      connect(mExpertAction, &QAction::triggered,
                              this, &MainWindow::customGame);
                  

                  The second should be mCustomAction

                  And also this:

                  mColorAction = new QAction{tr("C&olor"), this};
                    mColorAction->setCheckable(true);
                    mColorAction->setChecked(true);
                    connect(mColorAction, &QAction::triggered,
                            this, &MainWindow::toggleColor);
                  
                    mColorAction = new QAction{tr("&Best Times..."), this};
                    connect(mColorAction, &QAction::triggered,
                            this, &MainWindow::showBestTimes);
                  

                  The second should be mBestTimesAction

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

                  @sandro4912 said in Menus not showing up in MainWindow:

                  mGameMenu->addAction(mCustomAction);

                  In this case, in the code as shown your mGameMenu is fine/initialized, but the parameter mCustomAction is an uninitialized variable. A bad parameter to addAction() could doubtless cause a seg fault.

                  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