[Interest] Getting keypresses in a splashscreen on OSX

Glenn Ramsey gr at componic.co.nz
Tue May 20 00:08:08 CEST 2014


I worked around this by creating and showing a dummy window of zero size before 
creating and showing the splashscreen.

int main(int argc, char *argv[])
{
     QApplication a(argc, argv);

     // Create a dummy window so that we get keypresses on OSX
     QLabel v(0, Qt::FramelessWindowHint);
     v.setMinimumSize(QSize(0, 0));
     v.move(0,0);
     v.show();

     Splash *splash = new Splash;
     splash->setPixmap(QPixmap(":/images/splash_loading.png"));
     splash->show();
     splash->grabKeyboard();

     // keypresses are now captured here
     delay(5.f);

     v.hide();
     MainWindow w;
     w.show();
     ...

On 15/05/14 10:20, Glenn Ramsey wrote:
> Hi,
>
> 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?
>
> Glenn
>
> 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();
> }




More information about the Interest mailing list