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?
QtWS25 Last Chance

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 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?

    Pl45m4P 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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

        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?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on 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
        1
        • Pl45m4P Pl45m4

          @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 last edited by
          #3

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

          Pl45m4P 1 Reply Last reply
          0
          • A amy.cpp

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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on 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
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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

              • Login

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