Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add submenu in Qt

Tags:

qt

qt4

qt-creator

How do I add a submenu from the menu below? I need a submenu to open, say, after clicking

"A->Setup"

I want a submenu to be opened to the side of the main menu.

   void MyMenu::cppSlot()
        {
            QMenu *xmenu = new QMenu;
            xmenu->addMenu("A -> Setup");
            xmenu->addMenu("B -> Setup");
            xmenu->addMenu("C -> Setup");
            xmenu->addMenu("D -> Setup");
            xmenu->addMenu("E -> Setup");
            //Change font and width
            xmenu->setFont(QFont ("Courier", 10));
            xmenu->setFixedWidth(250);
            //Colour setting
            xmenu->setAutoFillBackground(true);
            /*QPalette palette=xmenu->palette();
            palette.setColor(QPalette::Window, Qt::black);
            palette.setColor(QPalette::Window, Qt::text);
            palette.color(green)
            xmenu->setPalette(palette);*/

            // Align the menu coordinates
           // xmenu->
            xmenu->move(900,300);

            xmenu->show();


        }
like image 359
kbk Avatar asked Dec 10 '12 10:12

kbk


People also ask

How do I add menu bar to QT?

To access this QMenuBar and populate it with things of your choice, just call menuBar() from your QMainWindow instance. To add a submenu to a QMenuBar , use QMenuBar::addMenu .

How do you use QMenu?

A horizontal QMenuBar just below the title bar of a QMainWindow object is reserved for displaying QMenu objects. QMenu class provides a widget which can be added to menu bar. It is also used to create context menu and popup menu. Each QMenu object may contain one or more QAction objects or cascaded QMenu objects.

What is the submenu?

Definition of submenu : a secondary menu (as in a computer application) : a list of choices that is part of another list of choices On selecting one of these sections, students should then be presented with a submenu which lists specific options related to the selected topic.—


1 Answers

QMenu::addMenu() returns a pointer to the created submenu. You can use these pointers to add actions for the submenus.

The following code:

QMenu *xmenu = new QMenu();
QMenu* submenuA = xmenu->addMenu( "A" );
QMenu* submenuB = xmenu->addMenu( "B" );
QMenu* submenuC = xmenu->addMenu( "C" );
QMenu* submenuD = xmenu->addMenu( "D" );
QMenu* submenuE = xmenu->addMenu( "E" );
QAction* actionA_Setup = submenuA->addAction( "Setup" );
QAction* actionB_Setup = submenuB->addAction( "Setup" );
QAction* actionC_Setup = submenuC->addAction( "Setup" );
QAction* actionD_Setup = submenuD->addAction( "Setup" );
QAction* actionE_Setup = submenuE->addAction( "Setup" );

(Hint: This cries for a loop)

will produce a menu like this:

Screenshot of the created menu

You can then connect slots to the triggered() signal of the returned actions (e.g. actionA_Setup).

like image 85
Tim Meyer Avatar answered Nov 01 '22 18:11

Tim Meyer