1

I've just coded splash screen in my PyQt application, to show an image before start. I've used QSplashscreen. The problem is the image is displayed, let's say, once in a 20 times. In other cases there is a grey rectangle displayed istead. Screenshots of both cases:

Does work: http://dl.getdropbox.com/u/1088961/prob2.jpg

Does not work: http://dl.getdropbox.com/u/1088961/prob1.jpg

I tried to delay starting window, but if grey rectangle changes into picture it is just before vanishing (even if I delay everything 10 seconds).

This is my code:

# -*- coding: utf-8 -*-
import sys
from time import time, sleep
from PyQt4.QtGui import QApplication, QSplashScreen, QPixmap

from gui.gui import MainWindow

def main():
    app = QApplication(sys.argv)
    start = time() 
    splash = QSplashScreen(QPixmap("aquaticon/images/splash_screen.jpg"))
    splash.show()
    if time() - start < 1:
        sleep(1)
    win = MainWindow()
    splash.finish(win)
    win.show()
    app.exec_()

if __name__ == "__main__":
    main()

I'm using Debian Linux with Fluxbox (but it is the same in Gnome).

daniel90
  • 13
  • 1
  • 8

1 Answers1

3

It's because of the sleep(1) line. For QSplashScreen to work properly, there should be an event loop running. However, sleep is blocking. So you don't get to app.exec_() (event loop) part before sleep finishes (for a whole second). That 'gray rectangle' is the case where you enter sleep before QSplashScreen could even paint itself.

For the normal case, you won't have this problem because you'll be waiting within Qt and the event loop will be running. If you want to 'simulate' a wait, sleep for small intervals and force the app to do its job with .processEvents():

# -*- coding: utf-8 -*-
import sys
from time import time, sleep
from PyQt4.QtGui import QApplication, QSplashScreen, QPixmap

from gui.gui import MainWindow

def main():
    app = QApplication(sys.argv)
    start = time() 
    splash = QSplashScreen(QPixmap("aquaticon/images/splash_screen.jpg"))
    splash.show()
    while time() - start < 1:
        sleep(0.001)
        app.processEvents()
    win = MainWindow()
    splash.finish(win)
    win.show()
    app.exec_()

if __name__ == "__main__":
    main()
Avaris
  • 35,883
  • 7
  • 81
  • 72
  • 1
    Note that QSplashScreen has quite a bad bug which means you just get a grey rectangle unless you have a main loop: https://bugreports.qt-project.org/browse/QTBUG-24910 – xioxox Jun 04 '13 at 18:56
  • Isn't it a bit of an overkill to uselessly wait a whole second to make sure the splash screen shows up? In my tests 30 ms was enough. – Ruslan Apr 26 '18 at 12:36
  • I agree with you @Ruslan – USERNAME GOES HERE Oct 29 '19 at 14:02