Accessing MainWindow Actions through shortcuts from a QDialog (modeless)



  • Hi,

    I have an action with a keyboard shortcut in my main window that starts a processing operation. But now I made a QDialog that pops up when a button is pressed, and in that dialog I set the value that the processing operation in the main window needs. So for this I made the dialog modeless but while I'm on the dialog the keyboard shortcuts of the mainwindow don't work until I actually click on the mainwindow again. Since I vary the parameter in the QDialog I would like to not need to always click on the mainwindow after setting a value in the dialog to start a "process".

    I tried doing:

    @
    ...
    processAction = new QAction(this)
    processAction->setShortcut(QKeySequence(Qt::Key_3));
    ...

    myDialog = new QDialog(this)
    myDialog->addAction(processAction);
    @

    But this didn't work. Is there a way to do this with dialogs??
    Thanks.



  • Hello Sergex,
    I tried to do something like this some time ago but in fact if the action is not used (for example in a toolBar or menu) is like it isn't activated, and does nothing. You can define a function inside the QDialog which manages QKeyEvents. You should re-implement the virtual function: @void keyPressEvent(QKeyEvent *);@ inside your QDialog. Then you can chose what to do when a certain key is pressed. For example:
    @void YourDialog::keyPressEvent(QKeyEvent *event) {
    if(event->key() == Qt::Key_3) { //your key
    (...)
    }
    }@ Hope this helps.
    Bye!
    Sebastian



  • Could you simply automatically reactivate the main window after showing the dialog, or is it important that the dialog be selected?



  • Did you try to create a [[Doc:QShortcut]] and set the shortcutContext to Qt::ApplicationShortcut?



  • Thanks for the suggestions!

    Seba84
    I tried this and I ran into a very strange behavior that I don't know how to resolve. I reimplemented the keyPressEvent of my dialog class precisely how you said and for some reason I noticed that the event never gets reached when I press any keys except the Shift, control or alt keys.
    I simply put a debug line like this:

    @
    void myDialog::keyPressEvent(QKeyEvent *event) {
    qDebug("Key Press Event Reached")
    if(event->key() == Qt::Key_3) {
    (...)
    }
    }
    @

    and I never get "Key Press Event Reached" by pressing keys unless I press any of the modifiers..

    Volker
    Yes, I tried your suggestion, I don't know if I'm doing something wrong but it didn't work for me.
    I did in my dialog class:

    @
    myShortcut = new QShortcut(this);
    myShortcut->setKey(Qt::Key_Space); //Or any other non modifier key
    myShortcut->setContext(Qt::ApplicationShortcut);
    connect(myShortCut, SIGNAL(activated()), this, SLOT(handleShortCutSlot()));
    @
    The handling slot is never entered in this case.

    I would really appreciate any further tips!

    Thanks.



  • The snippet works for me.

    But watch out! If you're in a text or line edit or any other widget that takes the space key for any action, the shortcut will not trigger!


Log in to reply
 

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