PyQT4.11
Python 2.7
I have been trying to create a splash screen to display a loading message when a specific tab is selected. When app.processEvents()
is called I get a grey box displayed instead of the image, but if I add a second app.processEvents()
after the first function is called the correct image is displayed until the splash is closed on completion.
I have played around with sleep timers, repainting, updating, closing and reshowing the tab and tabwidget with mixed results, on the rare occasion it will work but more often than not it won't. Can anyone shed any light on what I am missing?
import sys
from PyQt4 import QtCore, QtGui
class splashTest(QtGui.QDialog):
def __init__(self, parent=None):
super(splashTest, self).__init__(parent)
self.setGeometry(000,000,800,400)
self.tabWidget = QtGui.QTabWidget(self)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 500, 500))
self.tabWidget.setObjectName("tabWidget")
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.tab3 = QtGui.QWidget()
self.tabWidget.addTab(self.tab1, "tab1")
self.tabWidget.addTab(self.tab2, "tab2")
self.tabWidget.addTab(self.tab3, "Click Me")
self.tabWidget.setCurrentIndex(1)
self.tabWidget.currentChanged.connect(self.whichTabIsSelected)
def whichTabIsSelected(self):
if self.tabWidget.currentIndex() == 2:
splash_pix = QtGui.QPixmap('splash.png')
splash = QtGui.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
splash.show()
app.processEvents()
self.timeConsumingFunction()
app.processEvents()
self.timeConsumingFunction()
splash.finish(self.tabWidget)
else:
pass
def timeConsumingFunction(self):
b = 0
for a in range(100000000):
b += a
print (b)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = splashTest()
myapp.show()
sys.exit(app.exec_())