How to properly add a QMenu (and submenu) using a QAction

803 views Asked by At

I have been trying to add a submenu to a QMenu via QAction but it is not working. As you can see below I am able to see the choices via right click on a QTableWidget. However from the menu "Option" I should see the submenu, but I don't see it.

menu

Below the procedure:

rostreewidget.h

    class ROSTreeWidget : public QTreeWidget
    {
        Q_OBJECT

    public:
        ROSTreeWidget(QWidget *parent = nullptr);
    signals:
        void selectFrom();
        void editLaserTable();

    private:
        QAction *mActionSELECT_FROM;
        QAction *mActionEditLaserTable;
        QAction *mAddMenu;

        QActionGroup *actions1, *actions2;

        QMenu *mMenu;
        QMenu *submenu;
        QAction *actionA_Setup;


    };

rostreewidget.cpp

// Namespace ROSLaserItemTree
ROSTreeWidget::ROSTreeWidget(QWidget *parent) : QTreeWidget(parent)
{
    mActionSELECT_FROM = new QAction(QIcon("/home/mapper/execute-command"), "SELECT * FROM", this);
    mActionEditLaserTable = new QAction(QIcon(":"), "Edit LaserScan Table");
    mAddMenu = new QAction(QIcon(":"), "Options", this);

    addActions({ mActionSELECT_FROM,
                 mActionEditLaserTable,
                 mAddMenu});

    connect(mActionSELECT_FROM, &QAction::triggered, [&]() {
        emit selectFrom();
    });

    connect(mActionEditLaserTable, &QAction::triggered, [&]() {
        emit editLaserTable();
    });

    connect(mAddMenu, &QAction::triggered, [&]() {
        mMenu = new QMenu();
        submenu = mMenu->addMenu( "A" );
        QAction* actionA_Setup = submenu->addAction( "Setup" );
    });
}

What I have done so far:

1) After following this source I was able to prepare the menu and the submenu to be inserted in the QAction but unfortunately it is not working. As you can see from the print screen I am not able to see the choices under "Option".

2) Another thing I tried was going through this other source which was also clear, but still I could not figure out why the submenu is not being added in the QAction.

Thanks for pointing to the right direction for solving this issue.

0

There are 0 answers