Menu and subMenu



  • Hi,

    I have a main menu that I need to update it in run time, I want to add a submenu to it whenever a special node is added to the scene so that the user can see the node name in the submenu and do some actions based on that. I searched a lot but couldn't find a good way to do this in my application. I will be glad if someone can help me with it.

    Thanks!



  • You can use QAction to make new action and add it to the menu with addAction function and you can connect to triggered signal to do some action.
    @
    QAction *action = new QAction(tr("New Sub Menu"),this);
    action->setShortcut(Qt::CTRL | Qt::Key_M);
    connect(action, SIGNAL(triggerSignalHere()), this, SLOT(youSlotHere()));
    yourMainMenu->addAction(action);
    @



  • Thanks for the answer but I want to have sth like this:

    a main menu with the following:

    Delete
    Move to right
    Move to left
    Add to ....

    where add to... is a sub menu containing the following:

    "Add to ..." :
    processor 1
    processor 2
    .
    .
    .

    "Add to .. " menu is added in real-time whenever a new processor is created in the graphicsscene. and I have to avoid adding "Add to.. " multiple times.


  • Moderators

    Take a look at this example. Maybe it will help you:
    @
    //of course don't use globals, this is just an example
    QMenu* menu = new QMenu( someParent );
    menu->addAction("Delete");
    menu->addAction("Move to right");
    menu->addAction("Move to left");
    QAction* addEntry = menu->addAction("Add to...");

    QMenu* procmenu = new QMenu(...);
    addEntry->setMenu(procmenu);

    void addProcessor()
    {
    procmenu->addAction("Processor " + QString::number(procmenu->actions().size() + 1));
    }

    void showMenu(QPoint pt)
    {
    addEntry->setVisible( procmenu->actions().size() > 0 );
    menu->popup(pt);
    }

    //and then use it:
    addProcessor();
    addProcessor();
    showMenu(QPoint(x,y));
    @
    Similarly you can add removeProcessor() etc.



  • Thanks for the answer! it solved my problem! :)


Log in to reply
 

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