MainWindow is not taking the menubar's size into account
-
Hi all,
So the question here is, how can I make it so that QMainWindow resize itself to the "correct" width and height when a menubar is present? Because after adding the menu bar, the window's size still stays the same, resulting in parts of the widget not being shown. I'm wondering what's the best way to do it? What I have here is really dubious
I'm also using Qt5 if that helps
QApplication application (argc, argv); QMainWindow mainWindow; Game game(&mainWindow); QMenuBar* menuBar = mainWindow.menuBar(); QMenu* gameMenu = menuBar->addMenu("Game"); gameMenu->addAction(game.newGameAct); gameMenu->addAction(game.exitGameAct); gameMenu->addSeparator(); gameMenu->addAction(game.closeWindowAct); #ifdef Q_OS_WIN int menuBarHeight = menuBar->size().height() - 9; #else int menuBarHeight = menuBar->size().height() - 3; #endif mainWindow.resize(QSize (768, 550 + menuBarHeight)); game.resize(QSize(768, 550)); mainWindow.setCentralWidget(&game); mainWindow.show(); game.show(); return application.exec(); -
This post is deleted!
-
Hi all,
So the question here is, how can I make it so that QMainWindow resize itself to the "correct" width and height when a menubar is present? Because after adding the menu bar, the window's size still stays the same, resulting in parts of the widget not being shown. I'm wondering what's the best way to do it? What I have here is really dubious
I'm also using Qt5 if that helps
QApplication application (argc, argv); QMainWindow mainWindow; Game game(&mainWindow); QMenuBar* menuBar = mainWindow.menuBar(); QMenu* gameMenu = menuBar->addMenu("Game"); gameMenu->addAction(game.newGameAct); gameMenu->addAction(game.exitGameAct); gameMenu->addSeparator(); gameMenu->addAction(game.closeWindowAct); #ifdef Q_OS_WIN int menuBarHeight = menuBar->size().height() - 9; #else int menuBarHeight = menuBar->size().height() - 3; #endif mainWindow.resize(QSize (768, 550 + menuBarHeight)); game.resize(QSize(768, 550)); mainWindow.setCentralWidget(&game); mainWindow.show(); game.show(); return application.exec();@toara said in MainWindow is not taking the menubar's size into account:
Game game(&mainWindow);
Should crash on exit ...
Use new Game() instead.how can I make it so that QMainWindow resize itself to the "correct" width and height when a menubar is present?
Put Game in a layout and set it a fixed size:
game->setFixedSize(QSize(768, 550)); mainWindow.show(); // set the mainwindow a fixed size as well if you want to prevent resizing //mainWindow.setFixedSize(mainWindow.size()); qDebug()<<game->size(); // should be 768x550