I am trying to get keypresses from QSplashScreen before my main window opens. My splash class inherits from QSplashScreen and overrides the keyPressEvent method.
The code below works on Windows but on OSX the keypresses are not intercepted until the main window opens.
Is there a workaround for this?
This is using Qt 5.2.1, but I think this issue is also in earlier (4.X) versions too.
splash.cpp:
...
void Splash::keyPressEvent(QKeyEvent *evt)
{
std::cout << evt->text().toStdString() << std::endl;
}
main.cpp:
...
void delay(float seconds)
{
QTime dieTime= QTime::currentTime().addSecs(seconds);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Splash *splash = new Splash;
splash->setPixmap(QPixmap(":/images/splash_loading.png"));
splash->show();
splash->grabKeyboard();
// on OSX no keypresses captured here, on Windows keypresses captured
delay(5.f);
MainWindow w;
w.show();
// keypresses captured here on OSX and Windows
delay(5.f);
splash->releaseKeyboard();
splash ->hide();
return a.exec();
}