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. MenuBar - why it doesn't work?
Forum Update on Monday, May 27th 2025

MenuBar - why it doesn't work?

Scheduled Pinned Locked Moved Solved General and Desktop
menubarmenubegginer
5 Posts 3 Posters 1.0k Views
  • 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.
  • A Offline
    A Offline
    amy.cpp
    wrote on 19 Mar 2020, 17:15 last edited by
    #1

    Hi, I wrote this code:

    editor = new QTextEdit;
    setCentralWidget(editor);
    menu = new QMenuBar;
    listOfMenus = new QMenu[2];
    listOfMenus[0].setTitle("File");
    listOfMenus[1].setTitle("Edit");
    QString textes[] = {"New","Open","Save","Save as", "Print", "Exit",
                       "Undo", "Redo", "Copy","Paste","Cut"};
    QString icons[] = {"new","open","save","save","print","exit",
                      "undo", "redo", "copy", "paste", "cut"};
    
    QAction actions[11];
    
    for(int i=0; i<11; i++){
        actions[i].setIcon(QIcon(QString(":/Images/" + icons[i])));
        actions[i].setText(textes[i]);
    }
    
    for(int i=0; i<6; i++)
        listOfMenus[0].addAction(&actions[i]);
    
    for(int i=6; i<11; i++)
        listOfMenus[1].addAction(&actions[i]);
    
    menu->addMenu(listOfMenus);
    menu->addMenu(&listOfMenus[1]);
    menu->setVisible(true);
    setMenuBar(menu);
    

    It sets values of actions and then adds them into menus;
    and finally, menus are added to menu bar

    Unfortunately, code doesn't work... Do you know what's wrong?

    P 1 Reply Last reply 19 Mar 2020, 17:42
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 19 Mar 2020, 18:05 last edited by mrjj
      #5

      Hi
      I think you action runs out of scope

      it seems to be local variable
      QAction actions[11];

      and you take address of them so it does not copy but refernce inside the list
      listOfMenus[0].addAction(&actions[i]);

      but it looks to me like the actions[11]; lives in constructor and hence
      when Mainwindow is shown, they are all already deleted.

      Move the list to be a member of the class and see.

      Damn Pl45m4 beat me to it :)

      1 Reply Last reply
      3
      • A amy.cpp
        19 Mar 2020, 17:15

        Hi, I wrote this code:

        editor = new QTextEdit;
        setCentralWidget(editor);
        menu = new QMenuBar;
        listOfMenus = new QMenu[2];
        listOfMenus[0].setTitle("File");
        listOfMenus[1].setTitle("Edit");
        QString textes[] = {"New","Open","Save","Save as", "Print", "Exit",
                           "Undo", "Redo", "Copy","Paste","Cut"};
        QString icons[] = {"new","open","save","save","print","exit",
                          "undo", "redo", "copy", "paste", "cut"};
        
        QAction actions[11];
        
        for(int i=0; i<11; i++){
            actions[i].setIcon(QIcon(QString(":/Images/" + icons[i])));
            actions[i].setText(textes[i]);
        }
        
        for(int i=0; i<6; i++)
            listOfMenus[0].addAction(&actions[i]);
        
        for(int i=6; i<11; i++)
            listOfMenus[1].addAction(&actions[i]);
        
        menu->addMenu(listOfMenus);
        menu->addMenu(&listOfMenus[1]);
        menu->setVisible(true);
        setMenuBar(menu);
        

        It sets values of actions and then adds them into menus;
        and finally, menus are added to menu bar

        Unfortunately, code doesn't work... Do you know what's wrong?

        P Offline
        P Offline
        Pl45m4
        wrote on 19 Mar 2020, 17:42 last edited by
        #2

        @amy-cpp

        Hi,

        what exactly does not work?

        Better use container types like QList or QVector instead of std::arrays to store QObjects.


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

        ~E. W. Dijkstra

        A 1 Reply Last reply 19 Mar 2020, 17:47
        1
        • P Pl45m4
          19 Mar 2020, 17:42

          @amy-cpp

          Hi,

          what exactly does not work?

          Better use container types like QList or QVector instead of std::arrays to store QObjects.

          A Offline
          A Offline
          amy.cpp
          wrote on 19 Mar 2020, 17:47 last edited by
          #3

          @Pl45m4 Code creates menu bar, and menus 1 and 2, but doesn't add actions to menus

          P 1 Reply Last reply 19 Mar 2020, 18:04
          0
          • A amy.cpp
            19 Mar 2020, 17:47

            @Pl45m4 Code creates menu bar, and menus 1 and 2, but doesn't add actions to menus

            P Offline
            P Offline
            Pl45m4
            wrote on 19 Mar 2020, 18:04 last edited by
            #4

            @amy-cpp

            Create QActions on heap and use pointers.


            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
            3
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 19 Mar 2020, 18:05 last edited by mrjj
              #5

              Hi
              I think you action runs out of scope

              it seems to be local variable
              QAction actions[11];

              and you take address of them so it does not copy but refernce inside the list
              listOfMenus[0].addAction(&actions[i]);

              but it looks to me like the actions[11]; lives in constructor and hence
              when Mainwindow is shown, they are all already deleted.

              Move the list to be a member of the class and see.

              Damn Pl45m4 beat me to it :)

              1 Reply Last reply
              3

              2/5

              19 Mar 2020, 17:42

              • Login

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