Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Best way to duplicate context menu to main menu



  • In context menu actions are generated on show in CodeEditor::contextMenuEvent, but I don't know how works shortucts before showing of context menu.
    My piece of code:

    
        action = new QAction(tr("&Copy"), this);
        action->setShortcut(QKeySequence(QKeySequence::Copy));
        connect(action, SIGNAL(triggered()), this, SLOT(copy()));
        action->setEnabled(textCursor().hasSelection());
        selectionMenu->addAction(action);
    
        action = new QAction(tr("&Paste"), this);
        action->setShortcut(QKeySequence(QKeySequence::Paste));
        connect(action, SIGNAL(triggered()), this, SLOT(paste()));
        action->setEnabled(canPaste());
        selectionMenu->addAction(action);
    

    Now, I want copy Copy,Paste and other actions to main menu. How do it? I must have copy action for all open editors and fill main menu on show? or main menu actions call MainWindow proxy procedure with parameter, which call actions for editor?


  • Qt Champions 2017

    Do you want to the same action to be displayed for menu & context menu ? Is this you want ? If yes, simply add action object to both main menu and addAction for widget.


  • Lifetime Qt Champion

    Hi,

    If you want to use the same actions in multiple places, make them private members of your class and create them in your main window constructor.

    Then re-use the same actions for your contextual menu.


Log in to reply