Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Accessing MainWindow Actions through shortcuts from a QDialog (modeless)
Forum Updated to NodeBB v4.3 + New Features

Accessing MainWindow Actions through shortcuts from a QDialog (modeless)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sergex
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seba84
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris H
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sergex
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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!

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved