edbee-lib
edbee-lib copied to clipboard
How to set a shortcut on a custom menu item?
I've got the following for the context menu:
void dlgTriggerEditor::slot_editorContextMenu()
{
// retrieve the current controller and editor
edbee::TextEditorWidget* editor = mpSourceEditorEdbee;
if (!editor) {
return;
}
edbee::TextEditorController* controller = mpSourceEditorEdbee->controller();
// create the menu
auto menu = new QMenu();
menu->addAction(controller->createAction("cut", tr("Cut"), QIcon(), menu));
menu->addAction(controller->createAction("copy", tr("Copy"), QIcon(), menu));
menu->addAction(controller->createAction("paste", tr("Paste"), QIcon(), menu));
menu->addSeparator();
menu->addAction(controller->createAction("sel_all", tr("Select All"), QIcon(), menu));
auto formatAction = new QAction(QIcon(), tr("Format All"), menu);
formatAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J));
connect(formatAction, &QAction::triggered, [=]() {
auto formattedText = mpHost->mLuaInterpreter.formatLuaCode(mpSourceEditorEdbeeDocument->text());
qDebug() << formattedText;
mpSourceEditorEdbeeDocument->setText(formattedText);
});
menu->addAction(formatAction);
menu->exec(QCursor::pos());
delete menu;
}
While the cut/copy/paste shortcuts work fine, my custom one isn't. I suspect it's because it's not going through edbee's key mapping thing. Any ideas?
You should create the FormatAllAction before the context menu is shown. And just add it to the QWidget (I think the key combination should work all the time. Even without context menu)
edbeeWidget->addAction( formatAction)
To my knowledge the standard widget QAction keyboard shortcuts also work.