Search code examples
qtpyqtpy2app

Qt: MainWindow does not appear until application is activated manually


In my (Py)Qt4 application, the main window doesn't appear until the user uses command-tab to switch away from the app and then command-tabs back to it.

The strange thing is that this problem only shows itself when launched by double-clicking my .app in the Finder. If I launch the app from the command line, it works correctly.

Also, I've tried adding the following lines to my code, but nothing seems to do the trick:

mainWindow.show()
mainWindow.raise_()
mainWindow.repaint()
QApplication.postEvent(QEvent.ApplicationDeactivate)
QApplication.postEvent(QEvent.ApplicationActivate)
QApplication.instance().processEvents()

I thought that maybe this was somehow a thread-related issue, but I can insert this line pretty much anywhere:

assert threading.current_thread().name == "MainThread"

One final issue: I don't know if it's related, but my splash screen doesn't hide itself correctly (despite calling splashscreen.finish(mainWindow)). (If I comment out the splash screen entirely, the main window still misbehaves as described above.)

Why does my application bundle behave differently than launching from the command-line? Any idea how I can diagnose this? I use py2app to generate my application bundle.

Edit: This mailing list item explains the problem, gives a minimal test case. SPOILER: It turns out the argv_emulation option is the problem. It's not supposed to be used with GUI apps.


Solution

  • The issue here turned out to be that I was using the argv_emulation option, which isn't intended for use with GUI apps. Setting that option to False fixes the problem.

    (See also: http://mail.python.org/pipermail/pythonmac-sig/2013-August/023992.html)

    (Also, see the comments in py2app/bootstrap/argv_emulation.py