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.