Search code examples
qtmenubarqmainwindow

Menubar is not showing on the second window


I am trying to build an application that after getting some user input on the first window, pops up another window and displays some results. However, even though the menubar is visible on the first window, the menubar does not appear on the second window. The two windows are objects of different classes, but both classes are inherited from QMainWindow.

I have tried using the menuBar() function which returns a pointer for the menubar to add menus (this works for the first window). I also tried creating a new menubar object which didn't help either.

//MapWindow.h

class MapWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MapWindow(QWidget *parent = nullptr);
    ~MapWindow();
private:
    QAction *vehicleAct;
    QAction *missionAct;
    QAction *backAct;
    QMenu *toolMenu;
};


//MapWindow.cpp

MapWindow::MapWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MapWindow)
{
    ui->setupUi(this);
    setWindowState(Qt::WindowMaximized);

    vehicleAct = new QAction("Vehicle Selection");
    vehicleAct->setShortcut(Qt::CTRL + Qt::Key_V);

    missionAct = new QAction("Mission Selection");
    missionAct->setShortcut(Qt::CTRL + Qt::Key_M);

    backAct = new QAction("Back");
    backAct->setShortcut(Qt::CTRL + Qt::Key_B);

    toolMenu = menuBar()->addMenu("Tools");
    toolMenu->addAction(vehicleAct);
    toolMenu->addAction(missionAct);
    toolMenu->addAction(backAct);
}

MapWindow::~MapWindow() {
    delete ui;
}

When I use the same code in the WelcomeWindow class which is also inherited from QMainWindow it works perfectly. However it doesn't even show a menubar in this second window.


Solution

  • I managed to find the problem. One of my widgets (QScrollArea) was located in the top left corner of the screen which was stopping the whole menubar from displaying for some reason. Moving the QScrollArea down a little bit has solved the problem.