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. Crash on menuBar() after setMenuWidget()
Qt 6.11 is out! See what's new in the release blog

Crash on menuBar() after setMenuWidget()

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.0k Views 1 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.
  • P Offline
    P Offline
    Paddle
    wrote on last edited by
    #1

    I'm adding an option for frameless window to an application.
    For this I wanted to use setMenuWidget. So in my mainWindow Ctor I'm doing :

    auto* titleBar = new CustomTitleBar(this);
        setMenuWidget(titleBar);
    

    Now the thing is that this is an option. So somewhere down the line I have :

    void MenuManager::setup(MenuItem* menuItems) const
    {
        QMenuBar* menuBar = getMainWindow()->menuBar();
        QMenu* mainMenu = nullptr;
        if (!menuBar) {
            auto* ctb = dynamic_cast<CustomTitleBar*>(getMainWindow()->menuWidget());
            if (ctb) {
                mainMenu = ctb->mainMenu();
            }
        // Then populate either the menubar or the mainMenu...
        }
    

    And the issue is that doing menuBar() crashes the application. I would have expected menuBar() to return nullptr after setMenuWidget has been used. What am I doing wrong?

    Pl45m4P Christian EhrlicherC 2 Replies Last reply
    0
    • P Paddle

      I'm adding an option for frameless window to an application.
      For this I wanted to use setMenuWidget. So in my mainWindow Ctor I'm doing :

      auto* titleBar = new CustomTitleBar(this);
          setMenuWidget(titleBar);
      

      Now the thing is that this is an option. So somewhere down the line I have :

      void MenuManager::setup(MenuItem* menuItems) const
      {
          QMenuBar* menuBar = getMainWindow()->menuBar();
          QMenu* mainMenu = nullptr;
          if (!menuBar) {
              auto* ctb = dynamic_cast<CustomTitleBar*>(getMainWindow()->menuWidget());
              if (ctb) {
                  mainMenu = ctb->mainMenu();
              }
          // Then populate either the menubar or the mainMenu...
          }
      

      And the issue is that doing menuBar() crashes the application. I would have expected menuBar() to return nullptr after setMenuWidget has been used. What am I doing wrong?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Paddle said in Crash on menuBar() after setMenuWidget():

      I would have expected menuBar() to return nullptr after setMenuWidget has been used.

      What made you think that?

      • https://doc.qt.io/qt-6/qmainwindow.html#menuBar
      • https://doc.qt.io/qt-6/qmainwindow.html#menuWidget

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paddle
        wrote on last edited by
        #3

        Thanks for the reply.
        Ok so menuBar creates a menubar when there is none.

        Please note that it still crashes. Which is bad.

        Why would I think that? Because this function looks like a getter. For instance menuWidget() does exactly what I expected menuBar would do. IMO it's bad that menuBar creates a menu.

        1 Reply Last reply
        0
        • P Paddle

          I'm adding an option for frameless window to an application.
          For this I wanted to use setMenuWidget. So in my mainWindow Ctor I'm doing :

          auto* titleBar = new CustomTitleBar(this);
              setMenuWidget(titleBar);
          

          Now the thing is that this is an option. So somewhere down the line I have :

          void MenuManager::setup(MenuItem* menuItems) const
          {
              QMenuBar* menuBar = getMainWindow()->menuBar();
              QMenu* mainMenu = nullptr;
              if (!menuBar) {
                  auto* ctb = dynamic_cast<CustomTitleBar*>(getMainWindow()->menuWidget());
                  if (ctb) {
                      mainMenu = ctb->mainMenu();
                  }
              // Then populate either the menubar or the mainMenu...
              }
          

          And the issue is that doing menuBar() crashes the application. I would have expected menuBar() to return nullptr after setMenuWidget has been used. What am I doing wrong?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Paddle said in Crash on menuBar() after setMenuWidget():

          QMenuBar* menuBar = getMainWindow()->menuBar();

          Use a debugger and see where it crashes. Since getMainWindow() is your own function I would guess it returns a nullptr.

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

          1 Reply Last reply
          2

          • Login

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