且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Qt中动态添加菜单

更新时间:2023-11-29 23:12:22

我不确定您到底想在Move()插槽中做什么.

但这是您自己的代码(我删除了对我来说似乎无用的代码),并对其进行了修改,以使其不会在我的计算机上崩溃:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QAction>
#include <QMenu>

class MainWindow : public QMainWindow
{
   Q_OBJECT

public:
   explicit MainWindow(QWidget *parent = 0);

private:
   QMenu* menu;
   QAction *dummyaction;
   QMenu* m_pSubMenu;
 private slots:
    void Move();
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"

#include <QMenuBar>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
   m_pSubMenu = NULL;
   QMenuBar* pMenuBar = new QMenuBar(this);

   setMenuBar(pMenuBar);

   dummyaction = new QAction("Testing",this);
   menu = new QMenu("Test", this);
   menu->addAction(dummyaction);
   this->menuBar()->addMenu(menu);

   connect(menu, SIGNAL(aboutToShow()), this, SLOT(Move()));
}

void MainWindow::Move() {
   if (!m_pSubMenu) {
      m_pSubMenu = new QMenu(menu);
      dummyaction->setMenu(m_pSubMenu);
   }
   QAction* pAction = new QAction("Test", this);
   m_pSubMenu->addAction(pAction);
}

我不确定您要在Move()插槽中做什么,但是例如,每次调用Move()插槽时,都会添加一个新的子菜单项.

希望这会有所帮助.

I want to add, submenu to a menu item dynamically. How can I achive this?

I tried like this, I have created an Action and submenu. Then I have added the submenu to action. But, I have connected the "triggered" signal of action. its getting crash if I click on the action..

I have also handled the "aboutToShow" signal of menu, same its also getting crash when I click on action..

Here is the sampe code.

Submenu = new QMenu(this);      
connect(Submenu, SIGNAL( aboutToShow()), this, SLOT(move ()));

                  QAction *test = new QAction(tr("Selection"), this);
                  test ->setMenu(Submenu);

                 menubar()->addAction(test);

I want to get the notification, before the display of submenu..

additonal information:

pleas try this code, in your main window constructor.

QAction *action = new QAction("Test",this);
QAction *dummyaction = new QAction("Testing",this);
QMenu *menu = new QMenu();
menu->addAction(dummyaction);

bool val= connect(menu, SIGNAL( aboutToShow()), this, SLOT( Move()));
val= connect(menu, SIGNAL( aboutToHide()), this, SLOT(Move()));

action->setMenu(menu);
this->menuBar()->addAction(action);

if I do like this, I am able to see one submenu item. But before that Move slot should call, its not getting called.. and even before hide also the same slot should call.. its not coming..

I tried the return values of connect.. its true only… so what is wrong with my code.. please say..

I'm not sure to understand exactly what you're willing to do into your Move() slot.

But here is your own code (I removed what seemed useless to me), modified so that it is not crashing on my computer :

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QAction>
#include <QMenu>

class MainWindow : public QMainWindow
{
   Q_OBJECT

public:
   explicit MainWindow(QWidget *parent = 0);

private:
   QMenu* menu;
   QAction *dummyaction;
   QMenu* m_pSubMenu;
 private slots:
    void Move();
};

#endif // MAINWINDOW_H

mainwindow.cpp :

#include "mainwindow.h"

#include <QMenuBar>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
   m_pSubMenu = NULL;
   QMenuBar* pMenuBar = new QMenuBar(this);

   setMenuBar(pMenuBar);

   dummyaction = new QAction("Testing",this);
   menu = new QMenu("Test", this);
   menu->addAction(dummyaction);
   this->menuBar()->addMenu(menu);

   connect(menu, SIGNAL(aboutToShow()), this, SLOT(Move()));
}

void MainWindow::Move() {
   if (!m_pSubMenu) {
      m_pSubMenu = new QMenu(menu);
      dummyaction->setMenu(m_pSubMenu);
   }
   QAction* pAction = new QAction("Test", this);
   m_pSubMenu->addAction(pAction);
}

I don't know exactly what you want to do into your Move() slot, but as an example, each time the Move() slot is called, a new submenu item is added.

Hope this helps.