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

One Shortcut for all Actions in a menu



  • Hello guys,
    i am pretty noob in Qt things and hope you can help me with my problem.

    My menu in the menubar is Camera.
    Inside i have a submenu called "Mode".
    In Mode i have 3 actions: follow main object, detached and follow specified object.
    Now i want that if the user press "C" => the camera mode will switch in a loop like : main -> detached -> specified -> main ...

    I can´t find anything like that via Google.
    Thanks in advance,
    Vince



  • Hi, seems a QShortcut is enough for you.



  • thanks for your answer. i will try to find out how to get it works with QShortcut.

    Edit:
    I can't find out the way, how to activate 3 actions with 1 shortcut using QShortcut. Can you pls detail the hint?

    !http://www.bilder-upload.eu/upload/9b5d94-1411723606.jpg(like this)!



  • i found the answer:
    if you want to do that you can write a protected method. looks like this:
    @void MainWindow::keyPressEvent( QKeyEvent *e )
    {
    bool consumed = false;
    switch ( e->key() )
    {
    case Qt::Key_C:
    case 0x63:
    if(followCarAct->isChecked())
    detachedCameraAct->trigger();
    else
    if(detachedCameraAct->isChecked())
    followClientAct->trigger();
    else
    followCarAct->trigger();
    consumed= true;
    break;
    }
    if(consumed)
    e->accept();
    }@

    To write the shortcut to the menu, you only have to write the code like this:
    @cameraModeMenu = cameraMenu->addMenu(tr("&Switch camera mode\tC "));@

    Source:
    http://stackoverflow.com/questions/9109952/how-to-specify-mnemonics-ampersand-shortcut-to-qactions-in-a-qmenu



  • The method above works for Linux and Windows but not for mac.
    So if anyone has an other idea, i would appreciate to read it.


Log in to reply