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. MainWindow is not taking the menubar's size into account
Forum Updated to NodeBB v4.3 + New Features

MainWindow is not taking the menubar's size into account

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 217 Views 2 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.
  • T Offline
    T Offline
    toara
    wrote on last edited by
    #1

    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();
    
    M 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • T toara

        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();
        
        M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #3

        @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
        
        1 Reply Last reply
        0

        • Login

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