Results 1 to 3 of 3

Thread: how to catch keyPressEvents on Widgets when MainWindow is hidden

  1. #1
    Join Date
    Aug 2018
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default how to catch keyPressEvents on Widgets when MainWindow is hidden

    Hi all,

    I'm working on a QtWidgets (Qt 5.11) based Desktop Application on Mac OS.

    The MainWindow creates a custom QWidget that reimplements keyPressEvent:

    Qt Code:
    1. void QuickInterface::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Up)
    4. {
    5. qDebug() << "Up pressed";
    6. emit editorSelected();
    7. }
    8. else if(event->key() == Qt::Key_Down)
    9. {
    10. qDebug() << "Down pressed";
    11. }
    12. else if(event->key() == Qt::Key_Left)
    13. {
    14. qDebug() << "Left pressed";
    15. }
    16. else if(event->key() == Qt::Key_Right)
    17. {
    18. qDebug() << "Right pressed";
    19. }
    20. else if(event->key() == Qt::Key_Escape)
    21. {
    22. qDebug() << "Escape pressed";
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    The code is working as expected unless the MainWindow is minimized to the tray. Although the widget is still showing normal and has Focus and the qApplication is the currently active Desktop Application the KeyEvents are being ignored.

    Any ideas on what I could be missing? I don't understand the reason for this dependency. Can I change this behavior somehow, maybe by reimplementing the showMinimized() Slot of QMainWindow?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,139
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to catch keyPressEvents on Widgets when MainWindow is hidden

    The MainWindow creates a custom QWidget that reimplements keyPressEvent
    Is this widget created as a child of MainWindow (that is, do you pass a MainWindow pointer in to its constructor)? If so, try creating it as a top-level widget (e.i. with no explicit parent).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2018
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch keyPressEvents on Widgets when MainWindow is hidden

    Quote Originally Posted by d_stranz View Post
    Is this widget created as a child of MainWindow (that is, do you pass a MainWindow pointer in to its constructor)? If so, try creating it as a top-level widget (e.i. with no explicit parent).
    Hi, sorry for my late reply!

    It didn't make any difference, but I found the problem myself. I had set Qt::Popup WindowFlag for the Widget (sorry for not posting the code, forgot about it). Qt::Popup makes the Widget hide when it looses focus (by clicking outside).

    Unfortunately it also makes the Widget ignore KeyPressEvents when the MainWindow is minimized.

    You can reproduce it:

    Create a new QtCreator project. Create a custom widget (HotKeyWidget) and reimplement keyPressEvent like this.

    Qt Code:
    1. void HotKeyWidget::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Down)
    4. {
    5. qDebug() << "Down pressed -> MainWindow minimized";
    6. emit minimizeMainWin(); // define signal in header
    7.  
    8. } else if(event->key() == Qt::Key_Left)
    9. {
    10. qDebug() << "Left pressed -> Widget WindowFlag Qt::Popup set";
    11. this->setWindowFlag(Qt::Popup); // setting the window flag initially hides the window (intended?)
    12. this->showNormal(); // show the widget again
    13. }
    14. else
    15. qDebug() << "KeyPressEvent";
    16. }
    To copy to clipboard, switch view to plain text mode 

    In the MainWindow constructor:

    Qt Code:
    1. this->setWindowTitle("MainWindow");
    2. HotKeyWidget *hkWidget = new HotKeyWidget(); // making this a child doesn't change behavior
    3. QObject::connect(hkWidget, SIGNAL(minimizeMainWin()), this, SLOT(showMinimized()));
    4. hkWidget->setWindowTitle("hkWidget");
    5. hkWidget->showNormal();
    To copy to clipboard, switch view to plain text mode 

    1. Run the application and bring MainWindow and Widget to front. Click Widget to set Focus
    2. Press any key to see KeyPressEvent Debug Message. (Works as expected)
    3. Press Down Key to minimize MainWindow.
    4. Press any key to see KeyPressEvent Debug Message. (Works as expected even if MainWindow is minimized)
    5. Show MainWindow normal (unminimize) and click Widget again to set focus.
    6. Press Left Key to set Qt:Popup WindowFlag to the widget.
    7. Press any key to see KeyPressEvent Debug Message. (Works as expected even if Qt:Popup is set)
    8. Press Down Key to minimize MainWindow again.
    9. Press any other key and see KeyEvents are being ignored.

    So it turns out that the combination of setting Qt::Popup WindowFlag to the Widget and minimizing MainWindow makes the Widget ignore KeyEvents. Looks like a bug doesn't it? Did not test it on windows yet.

Similar Threads

  1. Replies: 4
    Last Post: 11th February 2018, 17:47
  2. Keybord shortcuts for hidden widgets / actions
    By Scope in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2016, 08:44
  3. horizontalSlider and hidden widgets
    By smemamian in forum Newbie
    Replies: 1
    Last Post: 29th June 2013, 22:16
  4. The delegated widgets become hidden
    By fulbay in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2010, 12:53
  5. widgets behind hidden widgets not working
    By bpetty in forum Newbie
    Replies: 13
    Last Post: 7th September 2007, 20:23

Tags for this Thread

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.