Cannot create Submenu's in Custom QMenu

  • I am new to this forum. And this is my first post. I am learning about the QMenu's. I got an example from google to create QMenu. It is working fine as expected. When i try to Sub class the QMenu and the use the sub classed menu in that example, then the Sub Menu's are not created. The program is working. but when i hover on the Nokia Developer menu, the sub menu is not shown. Herewith i have attached a sample pic of the required output. Can u please Help me out how to create sub menus using the Subclassed QMenu.


    @#ifndef SUBMENU_H
    #define SUBMENU_H

    #include <QMainWindow>
    #include "ui_SubMenu.h"
    #include "DerivedSubMenu.h"

    class SubMenu : public QMainWindow

    SubMenu(QWidget *parent = 0);

    private slots:
    void wikiForumNokia();
    void DiBoForumNokia();
    void nokiaIndia();
    void nokiaEurope();
    void help();
    void about();

    void createMyMenus();

    Ui::SubMenu ui;

    • DerivedSubMenu *nokiaMenu; //DerivedSubMenu is the Sub class of QMenu
      DerivedSubMenu forumNokiaMenu; //DerivedSubMenu is the Sub class of QMenu
    QAction *menu_nokiaIndiaAction;
    QAction *menu_nokiaEuropeAction;
    QAction *menu_wikiAction;
    QAction *menu_DiBoAction;
    QAction *menu_aboutAction;
    QAction *menu_helpAction;
    QAction *menu_exitAction;


    #endif // SUBMENU_H@


    @#include "SubMenu.h"
    #include <QMenu>
    #include <QMessageBox>
    #include <QDesktopServices>
    #include <QUrl>

    SubMenu::SubMenu(QWidget *parent)
    : QMainWindow(parent)
    setWindowTitle(tr("Sub Menus"));
    //Create memu and sub menu.

    void SubMenu::createMyMenus()

    //add action for help menu.
    menu_helpAction = new QAction(tr("Help"), this);
    connect(menu_helpAction, SIGNAL(triggered()), this, SLOT(help()));
    //add action for about menu.
    menu_aboutAction = new QAction(tr("About"), this);
    connect(menu_aboutAction, SIGNAL(triggered()), this, SLOT(about()));
    /* for nokia menu and its sub menus. */
    //create action Nokia India sub menu item.
    menu_nokiaIndiaAction = new QAction(tr("Nokia India"), this);
        connect(menu_nokiaIndiaAction, SIGNAL(triggered()), this, SLOT(nokiaIndia()));
        //create action Nokia Europe sum menu item
        menu_nokiaEuropeAction = new QAction(tr("Nokia Europe"), this);
    connect(menu_nokiaEuropeAction, SIGNAL(triggered()), this, SLOT(nokiaIndia()));
    //add menu item nokia to main menu
    *nokiaMenu = new DerivedSubMenu(this);* // creating menu with the Sub classed QMenu
    //add action for nokia india sub-menu
    //add action for nokia india sub-menu
    //create action for Discussion Boards sub menu item
    menu_DiBoAction = new QAction(tr("Discussion Boards"), this);
        connect(menu_DiBoAction, SIGNAL(triggered()), this, SLOT(DiBoForumNokia()));
       //create action for Wiki sub menu item
        menu_wikiAction = new QAction(tr("Wiki"), this);
    connect(menu_wikiAction, SIGNAL(triggered()), this, SLOT(wikiForumNokia()));
    //add menu item Nokia Developer to menu item nokia
    • forumNokiaMenu = new DerivedSubMenu(this);* // creating menu with the Sub classed QMenu
      forumNokiaMenu->addMenu("Nokia Developer");
      //forumNokiaMenu = nokiaMenu->addMenu(tr("Nokia Developer"));
      //add action for Discussion Boards sub-menu
      //add action for Wiki sub-menu
    //Add action for exit menu.
    menu_exitAction = new QAction(tr("Exit"), this);
    connect(menu_exitAction, SIGNAL(triggered()), this, SLOT(close()));


    /* handle action for wiki sub-menu item. */
    void SubMenu::wikiForumNokia()

    /* handle action for Discussion Boards sub-menu item. */
    void SubMenu::DiBoForumNokia()

    /* handle action for nokia india sub-menu item. */
    void SubMenu::nokiaIndia()

    /* handle action for nokia europe sub-menu item. */
    void SubMenu::nokiaEurope()

    /* handle action for help menu item. */
    void SubMenu::help()
    QMessageBox::information(this, tr("Help Menu"),tr("This example shows how to create submenu."));

    /* handle action for about menu item. */
    void SubMenu::about()
    QMessageBox::about(this, tr("About Menu"),tr("Testing Sub-Menus."));



  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are not adding any entry to your subclassed menu so it won't show anything. Look again on the original line you have commented out

  • Thanks for your reply. I am adding the entry to the sub classed menu as,

    from the example it is given as

    @//add menu item nokia to main menu
    nokiaMenu = menuBar()->addMenu(tr("Nokia"));@

    So i have added the menu directly by using addMenu() function.

    Am I adding it in the wrong way? Please correct me if i am.

  • Lifetime Qt Champion

    Sorry, I missed the first one.

    Why don't you give a name to your DerivedSubMenu ?

    Also, you don't add it to the menu bar, so it won't be shown

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.