Solved Menus not showing up in MainWindow
-
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); }
-
@sandro4912 said in Menus not showing up in MainWindow:
mNewAction = new QAction{tr("&New"), this};
Are you really using@Christian-Ehrlicher has explained below that this is yet another C++ initialization syntax....{ ... }
, not( ... )
, there?Break your problem down: start out with just the two
menuBar()->addMenu()
s, noQAction
s, and verify the menu shows up? Is it your top-level menus on the menubar or their action items which do not appear? -
@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?
Are you really using { ... }, not ( ... ), there?
It's allowed since c++11 - beautiful nice 'uniform initialization'... :/
-
@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 thevoid
from the front of these and your menus will appear :) -
one of these mistake you look at and don't see because they are very stupid. removing the void solved the issue as expected
-
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
-
@sandro4912 said in Menus not showing up in MainWindow:
mGameMenu
Did you assign a valid pointer to mGameMenu before using it?
-
@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 parametermCustomAction
is an uninitialized variable. A bad parameter toaddAction()
could doubtless cause a seg fault.