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. Second call to setMenuBar does nothing
Forum Updated to NodeBB v4.3 + New Features

Second call to setMenuBar does nothing

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 581 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.
  • O oneeyeman1
    3 Apr 2025, 04:54

    Hi, ALL,
    I'm trying to develop a program with wxWidgets onn top of Qt.

    I have somewhat outdated Qt version - 5.12 and the recent wxWidgets.

    I'm testIng on the Gentoo platform (Linux).

    The program I'm trying to develop is based on the MDI paradigm and so it has different views for a different types of documents. Every view should have its own menu bar.

    When the program starts - there is nothing. Only basic menu bar and the toolbar.
    When the user clicks the tool on the toolbar a new menu bar is made and should be set as well as new toolbar should be created.

    The toolbar is created, the view and the document are made, but the menu bar is not changed.

    It looks like the general recommendation is to have only one menu bar for the life time of the program. However it is not the case here.

    Basically I'm calling the setMenuBar() on the main frame, but the call has no effect.

    Does anybody know how to properly update the menu bar of the program with the brand new menu bar?

    Thank you.

    J Offline
    J Offline
    JonB
    wrote on 3 Apr 2025, 07:24 last edited by
    #4

    @oneeyeman1
    As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?

    O 1 Reply Last reply 3 Apr 2025, 14:45
    0
    • J JonB
      3 Apr 2025, 07:24

      @oneeyeman1
      As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?

      O Offline
      O Offline
      oneeyeman1
      wrote on 3 Apr 2025, 14:45 last edited by oneeyeman1 4 Mar 2025, 14:50
      #5

      @JonB said in Second call to setMenuBar does nothing:

      @oneeyeman1
      As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?

      Is there a siimple standalone Qt program I can try?

      wxWidgets do not do anything - just calls setMenuBar and that's it (after checking that existing and the new are not the same).

      There is no crash, no assert - menu bar is stays the same.

      Maybe there was a bug in 5.12?

      IgorsGentoo /home/igor/dbhandler_new/Debug # emerge -pv qtcore
       * Last emerge --sync was 3y 55d 17h 58m 58s ago.
      
      These are the packages that would be merged, in order:
      
      Calculating dependencies... done!
      [ebuild   R    ] dev-qt/qtcore-5.15.2-r14:5/5.15.2::gentoo  USE="icu -debug -old-kernel -systemd -test" 0 KiB
      
      Total: 1 package (1 reinstall), Size of downloads: 0 KiB
      
       * IMPORTANT: 20 news items need reading for repository 'gentoo'.
       * Use eselect news read to view new items.
      

      Thank you.

      J 1 Reply Last reply 3 Apr 2025, 14:56
      0
      • O oneeyeman1
        3 Apr 2025, 14:45

        @JonB said in Second call to setMenuBar does nothing:

        @oneeyeman1
        As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?

        Is there a siimple standalone Qt program I can try?

        wxWidgets do not do anything - just calls setMenuBar and that's it (after checking that existing and the new are not the same).

        There is no crash, no assert - menu bar is stays the same.

        Maybe there was a bug in 5.12?

        IgorsGentoo /home/igor/dbhandler_new/Debug # emerge -pv qtcore
         * Last emerge --sync was 3y 55d 17h 58m 58s ago.
        
        These are the packages that would be merged, in order:
        
        Calculating dependencies... done!
        [ebuild   R    ] dev-qt/qtcore-5.15.2-r14:5/5.15.2::gentoo  USE="icu -debug -old-kernel -systemd -test" 0 KiB
        
        Total: 1 package (1 reinstall), Size of downloads: 0 KiB
        
         * IMPORTANT: 20 news items need reading for repository 'gentoo'.
         * Use eselect news read to view new items.
        

        Thank you.

        J Offline
        J Offline
        JonB
        wrote on 3 Apr 2025, 14:56 last edited by
        #6

        @oneeyeman1
        You just write a small, standalone example Qt widgets program with setMenyBar(), calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.

        O 1 Reply Last reply 3 Apr 2025, 15:04
        1
        • J JonB
          3 Apr 2025, 14:56

          @oneeyeman1
          You just write a small, standalone example Qt widgets program with setMenyBar(), calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.

          O Offline
          O Offline
          oneeyeman1
          wrote on 3 Apr 2025, 15:04 last edited by
          #7

          @JonB said in Second call to setMenuBar does nothing:

          @oneeyeman1
          You just write a small, standalone example Qt widgets program with setMenyBar(), calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.

          Doesn't Qt comes with a set of sample code?
          The ones people can quickly build and test something if it doesn't work or just check something?

          Thank you.

          J 1 Reply Last reply 3 Apr 2025, 15:09
          0
          • O oneeyeman1
            3 Apr 2025, 15:04

            @JonB said in Second call to setMenuBar does nothing:

            @oneeyeman1
            You just write a small, standalone example Qt widgets program with setMenyBar(), calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.

            Doesn't Qt comes with a set of sample code?
            The ones people can quickly build and test something if it doesn't work or just check something?

            Thank you.

            J Offline
            J Offline
            JonB
            wrote on 3 Apr 2025, 15:09 last edited by JonB 4 Mar 2025, 15:14
            #8

            @oneeyeman1 said in Second call to setMenuBar does nothing:

            Doesn't Qt comes with a set of sample code?

            Qt comes with "hundreds" of examples, and the web is full of further "hundreds" of examples. I guess you could Google setMenuBar() if you want to try to find some code calling that.

            If I understand your claim right, something which includes

            QMenuBar *menuBar1, *menuBar2;
            menuBar1 = new QMenuBar();
            mainWindow->setMenuBar(menuBar1)
            menuBar2 = new QMenuBar();
            mainWindow->setMenuBar(menuBar2)
            

            with appropriate items added to the menubars (perhaps menuBar1->addMenu("menuBar1") etc,) so we can distinguish them does not work correctly, so I would start from that.

            O 1 Reply Last reply 3 Apr 2025, 15:50
            1
            • J JonB
              3 Apr 2025, 15:09

              @oneeyeman1 said in Second call to setMenuBar does nothing:

              Doesn't Qt comes with a set of sample code?

              Qt comes with "hundreds" of examples, and the web is full of further "hundreds" of examples. I guess you could Google setMenuBar() if you want to try to find some code calling that.

              If I understand your claim right, something which includes

              QMenuBar *menuBar1, *menuBar2;
              menuBar1 = new QMenuBar();
              mainWindow->setMenuBar(menuBar1)
              menuBar2 = new QMenuBar();
              mainWindow->setMenuBar(menuBar2)
              

              with appropriate items added to the menubars (perhaps menuBar1->addMenu("menuBar1") etc,) so we can distinguish them does not work correctly, so I would start from that.

              O Offline
              O Offline
              oneeyeman1
              wrote on 3 Apr 2025, 15:50 last edited by
              #9

              @JonB said in Second call to setMenuBar does nothing:

              @oneeyeman1 said in Second call to setMenuBar does nothing:

              Doesn't Qt comes with a set of sample code?

              Qt comes with "hundreds" of examples, and the web is full of further "hundreds" of examples. I guess you could Google setMenuBar() if you want to try to find some code calling that.

              If I understand your claim right, something which includes

              QMenuBar *menuBar1, *menuBar2;
              menuBar1 = new QMenuBar();
              mainWindow->setMenuBar(menuBar1)
              menuBar2 = new QMenuBar();
              mainWindow->setMenuBar(menuBar2)
              

              with appropriate items added to the menubars (perhaps menuBar1->addMenu("menuBar1") etc,) so we can distinguish them does not work correctly, so I would start from that.

              That code snippet you put in is the exact replica.
              What I'm claiming is that after the second call to setMenuBar() I will still see the first one displayed. (That's to clarify what's "not work correctly" means).

              I will try to find some small sample code to test. But I was hoping that you can pint me to some Qt sample from distribution that I can use for testing, as I presume they are the smallest possible ones.

              Than you.

              1 Reply Last reply
              0
              • O Offline
                O Offline
                oneeyeman1
                wrote on 3 Apr 2025, 16:17 last edited by
                #10

                Hi,
                I found the sample here: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/mainwindows/mdi?h=5.15. I will see if I can make t fail as I explained t.

                Now how can I build it?

                Just run:

                gcc -o mdi *.cpp

                ?

                I'd guess it will fail miserably... ;-)

                Thank you.

                J 1 Reply Last reply 3 Apr 2025, 17:28
                0
                • O oneeyeman1
                  3 Apr 2025, 16:17

                  Hi,
                  I found the sample here: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/mainwindows/mdi?h=5.15. I will see if I can make t fail as I explained t.

                  Now how can I build it?

                  Just run:

                  gcc -o mdi *.cpp

                  ?

                  I'd guess it will fail miserably... ;-)

                  Thank you.

                  J Offline
                  J Offline
                  JonB
                  wrote on 3 Apr 2025, 17:28 last edited by
                  #11

                  @oneeyeman1
                  You should install Qt Creator and open the .pro file as a (qmake) project. You need to set up your Compilation Kit, hopefully it will auto-detect the gcc stuff you have.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    oneeyeman1
                    wrote on 4 Apr 2025, 03:27 last edited by
                    #12

                    Hi guys,
                    I didn't install Qt-Creator.

                    Instead I ran "qmake". It generated Makefile.

                    Then I ran "make". And this is what I got:

                    igor@IgorsGentoo ~/sample $ make
                    g++ -c -pipe -O2 -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I/usr/lib64/qt5/mkspecs/linux-g++ -o main.o main.cpp
                    In file included from mainwindow.h:53,
                                     from mainwindow.h:53,
                    ................................................................
                                     from main.cpp:5:
                    mainwindow.h:51:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                       51 | #include <QtWidgets>
                          |                     ^
                    mainwindow.h:53:24: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                       53 | #include "mainwindow.h"
                          |                        ^
                    mainwindow.h:54:22: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                       54 | #include "mdichild.h"
                          |                      ^
                    In file included from mainwindow.h:54,
                    ......................................................................................................
                                     from mainwindow.h:53,
                                     from main.cpp:5:
                    mdichild.h:54:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                       54 | #include <QTextEdit>
                          |                     ^
                    ^Cmake: *** wait: No child processes.  Stop.
                    make: *** Waiting for unfinished jobs....
                    make: *** wait: No child processes.  Stop.
                    igor@IgorsGentoo ~/sample $
                    

                    How do I fix it?

                    Thank you.

                    J 1 Reply Last reply 4 Apr 2025, 07:35
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 4 Apr 2025, 04:25 last edited by
                      #13

                      Don't recursivly include headers, use include guards to make sure the header is not included more than once. Basic c++ stuff, nothing Qt specific.

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

                      O 1 Reply Last reply 4 Apr 2025, 15:36
                      2
                      • O oneeyeman1
                        4 Apr 2025, 03:27

                        Hi guys,
                        I didn't install Qt-Creator.

                        Instead I ran "qmake". It generated Makefile.

                        Then I ran "make". And this is what I got:

                        igor@IgorsGentoo ~/sample $ make
                        g++ -c -pipe -O2 -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I/usr/lib64/qt5/mkspecs/linux-g++ -o main.o main.cpp
                        In file included from mainwindow.h:53,
                                         from mainwindow.h:53,
                        ................................................................
                                         from main.cpp:5:
                        mainwindow.h:51:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                           51 | #include <QtWidgets>
                              |                     ^
                        mainwindow.h:53:24: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                           53 | #include "mainwindow.h"
                              |                        ^
                        mainwindow.h:54:22: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                           54 | #include "mdichild.h"
                              |                      ^
                        In file included from mainwindow.h:54,
                        ......................................................................................................
                                         from mainwindow.h:53,
                                         from main.cpp:5:
                        mdichild.h:54:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum)
                           54 | #include <QTextEdit>
                              |                     ^
                        ^Cmake: *** wait: No child processes.  Stop.
                        make: *** Waiting for unfinished jobs....
                        make: *** wait: No child processes.  Stop.
                        igor@IgorsGentoo ~/sample $
                        

                        How do I fix it?

                        Thank you.

                        J Offline
                        J Offline
                        JonB
                        wrote on 4 Apr 2025, 07:35 last edited by JonB 4 Apr 2025, 07:41
                        #14

                        @oneeyeman1
                        I do not know whether you made changes to that example or there is a problem with it (which I would doubt).

                        In any case, here is a completely standalone single source file program which changes the menubar:

                        #include <QApplication>
                        #include <QMainWindow>
                        #include <QMenuBar>
                        #include <QTimer>
                        
                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            QMainWindow w;
                        
                            QMenuBar *menuBar1 = new QMenuBar();
                            menuBar1->addMenu("menuBar1");
                            w.setMenuBar(menuBar1);
                        
                            QMenuBar *menuBar2 = new QMenuBar();
                            menuBar2->addMenu("menuBar2");
                            w.setMenuBar(menuBar2);
                        
                            w.show();
                        
                            QMenuBar *menuBar3 = new QMenuBar();
                            menuBar3->addMenu("menuBar3");
                            QTimer::singleShot(3000, [&w, menuBar3]() { w.setMenuBar(menuBar3); });
                        
                            return a.exec();
                        }
                        

                        Presumably you can compile and link this, perhaps just as you have done for any wxWidgets you currently have.

                        The code starts with one menubar, replaces it with a second one immediately, and then replaces it with a third after 3 seconds. Needless to say it works as expected for me (Ubuntu 22.04, Qt 6.x, and I would expect it to work with any Qt version including your Qt5). You might start by checking this for yourself. If you then think that, say, MDI is required to show a problem then it will need altering for that.

                        O 1 Reply Last reply 4 Apr 2025, 15:17
                        2
                        • J JonB
                          4 Apr 2025, 07:35

                          @oneeyeman1
                          I do not know whether you made changes to that example or there is a problem with it (which I would doubt).

                          In any case, here is a completely standalone single source file program which changes the menubar:

                          #include <QApplication>
                          #include <QMainWindow>
                          #include <QMenuBar>
                          #include <QTimer>
                          
                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              QMainWindow w;
                          
                              QMenuBar *menuBar1 = new QMenuBar();
                              menuBar1->addMenu("menuBar1");
                              w.setMenuBar(menuBar1);
                          
                              QMenuBar *menuBar2 = new QMenuBar();
                              menuBar2->addMenu("menuBar2");
                              w.setMenuBar(menuBar2);
                          
                              w.show();
                          
                              QMenuBar *menuBar3 = new QMenuBar();
                              menuBar3->addMenu("menuBar3");
                              QTimer::singleShot(3000, [&w, menuBar3]() { w.setMenuBar(menuBar3); });
                          
                              return a.exec();
                          }
                          

                          Presumably you can compile and link this, perhaps just as you have done for any wxWidgets you currently have.

                          The code starts with one menubar, replaces it with a second one immediately, and then replaces it with a third after 3 seconds. Needless to say it works as expected for me (Ubuntu 22.04, Qt 6.x, and I would expect it to work with any Qt version including your Qt5). You might start by checking this for yourself. If you then think that, say, MDI is required to show a problem then it will need altering for that.

                          O Offline
                          O Offline
                          oneeyeman1
                          wrote on 4 Apr 2025, 15:17 last edited by
                          #15

                          @JonB ,

                          When I try to compile my program I'm doing "../configure && make -j3".

                          I can copy the code you posted and just run make, but I presume it will fail as it need the include and library paths.

                          Can you give me a proper way to build?

                          Thank you.

                          1 Reply Last reply
                          0
                          • C Christian Ehrlicher
                            4 Apr 2025, 04:25

                            Don't recursivly include headers, use include guards to make sure the header is not included more than once. Basic c++ stuff, nothing Qt specific.

                            O Offline
                            O Offline
                            oneeyeman1
                            wrote on 4 Apr 2025, 15:36 last edited by
                            #16

                            @Christian-Ehrlicher

                            What's interesting is that the sample code I'm trying to compile is not using setMenuBar() call.

                            I wonder how it works without it...

                            Thank you.

                            J 1 Reply Last reply 4 Apr 2025, 15:49
                            0
                            • O oneeyeman1
                              4 Apr 2025, 15:36

                              @Christian-Ehrlicher

                              What's interesting is that the sample code I'm trying to compile is not using setMenuBar() call.

                              I wonder how it works without it...

                              Thank you.

                              J Offline
                              J Offline
                              JonB
                              wrote on 4 Apr 2025, 15:49 last edited by
                              #17

                              @oneeyeman1
                              You do not normally need setMenuBar(). QMainWindow creates one if you access menuBar(). Then again, I do not know of any code which swaps/uses multiple menubars, which you say is your use case.

                              O 1 Reply Last reply 5 Apr 2025, 03:06
                              0
                              • J JonB
                                4 Apr 2025, 15:49

                                @oneeyeman1
                                You do not normally need setMenuBar(). QMainWindow creates one if you access menuBar(). Then again, I do not know of any code which swaps/uses multiple menubars, which you say is your use case.

                                O Offline
                                O Offline
                                oneeyeman1
                                wrote on 5 Apr 2025, 03:06 last edited by
                                #18

                                @JonB ,

                                Imagine you are working in the IDE,

                                You are wring a source code and then realize you want too add some bitmap.

                                You go to add one, but you menu bar needs to change accordingly.

                                Thais just one example. In the MDI world its actually expected to change menu bar in accordance to the document you have open.

                                Thank you.

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 5 Apr 2025, 06:55 last edited by Christian Ehrlicher 24 days ago
                                  #19

                                  This is mostly done through https://doc.qt.io/qt-6/qaction.html#visible-prop and https://doc.qt.io/qt-6/qaction.html#enabled-prop but not by replacing the entire menu bar...
                                  Please provide a minimal, compilable example to reproduce your problem (without wxwhatever) - @JonB 's example works fine so proove us and Qt wrong.

                                  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

                                  13/19

                                  4 Apr 2025, 04:25

                                  • Login

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