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.

    ![IMG]http://i.imgur.com/608y0r3.png/IMG!

    SubMenu.h
    @#ifndef SUBMENU_H
    #define SUBMENU_H

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

    class SubMenu : public QMainWindow
    {
    Q_OBJECT

    public:
    SubMenu(QWidget *parent = 0);
    ~SubMenu();

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

    private:
    void createMyMenus();

    private:
    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@

    SubMenu.cpp

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

    SubMenu::SubMenu(QWidget *parent)
    : QMainWindow(parent)
    {
    ui.setupUi(this);
    setWindowTitle(tr("Sub Menus"));
    //Create memu and sub menu.
    createMyMenus();
    }

    void SubMenu::createMyMenus()
    {

    //add action for help menu.
    menu_helpAction = new QAction(tr("Help"), this);
    menuBar()->addAction(menu_helpAction);
    connect(menu_helpAction, SIGNAL(triggered()), this, SLOT(help()));
    
    //add action for about menu.
    menu_aboutAction = new QAction(tr("About"), this);
    menuBar()->addAction(menu_aboutAction);
    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
    nokiaMenu->addMenu("Nokia");
    //add action for nokia india sub-menu
    nokiaMenu->addAction(menu_nokiaIndiaAction);
    //add action for nokia india sub-menu
    nokiaMenu->addAction(menu_nokiaEuropeAction);
    
    //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
      forumNokiaMenu->addAction(menu_DiBoAction);
      //add action for Wiki sub-menu
      forumNokiaMenu->addAction(menu_wikiAction);
    //Add action for exit menu.
    menu_exitAction = new QAction(tr("Exit"), this);
    menuBar()->addAction(menu_exitAction);
    connect(menu_exitAction, SIGNAL(triggered()), this, SLOT(close()));
    

    }

    /* handle action for wiki sub-menu item. */
    void SubMenu::wikiForumNokia()
    {
    QDesktopServices::openUrl(QUrl(tr("http://www.developer.nokia.com/Community/Wiki/Wiki_Home")));
    }

    /* handle action for Discussion Boards sub-menu item. */
    void SubMenu::DiBoForumNokia()
    {
    QDesktopServices::openUrl(QUrl(tr("http://www.developer.nokia.com/Community/Discussion/")));
    }

    /* handle action for nokia india sub-menu item. */
    void SubMenu::nokiaIndia()
    {
    QDesktopServices::openUrl(QUrl(tr("http://www.nokia.co.in/")));
    }

    /* handle action for nokia europe sub-menu item. */
    void SubMenu::nokiaEurope()
    {
    QDesktopServices::openUrl(QUrl(tr("http://europe.nokia.com/home")));
    }

    /* 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."));
    }

    SubMenu::~SubMenu()
    {

    }@


  • 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,
    @nokiaMenu->addMenu("Nokia");@

    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.