Results 1 to 4 of 4

Thread: Keeping Shortcuts active in the MainWindow when on a modeless QDialog

  1. #1
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Keeping Shortcuts active in the MainWindow when on a modeless QDialog

    Hi,

    I have an action with a keyboard shortcut in my main window that starts an operation called "process". But now I made a QDialog that pops up when a button is pressed, and in that dialog I set the value that the process 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:

    Qt Code:
    1. //In main window
    2.  
    3. ...
    4. processAction = new QAction(this)
    5. processAction->setShortcut(QKeySequence(Qt::Key_3));
    6. ...
    7.  
    8. myDialog = new QDialog(this)
    9. myDialog->addAction(processAction);
    To copy to clipboard, switch view to plain text mode 

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

    Thanks.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Keeping Shortcuts active in the MainWindow when on a modeless QDialog

    Event Filter to the rescue!
    Install it on the dialog with main window as a filtering object and you're good to go.

  3. #3
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Keeping Shortcuts active in the MainWindow when on a modeless QDialog

    Thanks for the reply!

    Yes this almost works, almost because I'm having a strange problem happening. I installed the eventFilter on the dialog like this:


    Qt Code:
    1. //In my MainWindow
    2.  
    3. myDialog->installEventFilter(this);
    4. ...
    5.  
    6. ...
    7. bool eventFilter(QObject *obj, QEvent *e)
    8. {
    9. if(obj == myDialog)
    10. {
    11. if(e->type() == QEvent::KeyPress)
    12. {
    13. QKeyEvent* keyEvent = static_cast<QKeyEvent*>(e);
    14. if(keyEvent->key() == Qt::Key_3)
    15. {
    16. process();
    17. return true;
    18. }
    19. else
    20. return false;
    21. }
    22. }
    23.  
    24. return QMainWindow::eventFilter(obj, e);
    25. }
    To copy to clipboard, switch view to plain text mode 


    This will only work if I set the 'if' line :
    if(keyEvent->key() == Qt::Key_Shift)
    ..

    In other words it will only go into the if(e->type() == QEvent::KeyPress), only if with the SHIFT, Control or Alt keys are presssed but not with any other keys. And since I don't want any of the modifier keys as shortcut to my function I am still stuck.

    I am on Mac OS 10.6.8 if that makes any difference..

    Any ideas??

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Keeping Shortcuts active in the MainWindow when on a modeless QDialog

    I don't have mac at hand to check, but I would guess this shouldn't make any difference.

    Qt Code:
    1. bool MainWindow::eventFilter( QObject* o, QEvent* e )
    2. {
    3. if( e->type() == QEvent::KeyPress )
    4. {
    5. QKeyEvent* keyEvent = static_cast<QKeyEvent*>( e );
    6. qDebug() << keyEvent->key();
    7. }
    8. return QMainWindow::eventFilter( o, e );
    9. }
    To copy to clipboard, switch view to plain text mode 
    should work for any key as long as the window has the focus (it works in my case, but I'm running w7 box at the moment).

Similar Threads

  1. Replies: 4
    Last Post: 15th September 2010, 00:22
  2. QFileDialog in modeless QDialog and mutex error
    By enno in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2010, 20:49
  3. Raising the MainWindow to active/focused state
    By bmahf in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2009, 21:15
  4. QDialog modeless problem
    By sgtbash in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2009, 19:03
  5. MainWindow in front of child modeless dialog
    By jiveaxe in forum Qt Programming
    Replies: 4
    Last Post: 10th August 2007, 16:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.