MObject
MObject

Reputation: 397

Splashscreen display

I'm trying to display a pop up widget during a code execution and destroy it at the end of the execution.

from PySide import QtGui, QtCore
import sys
import time
def spinner(fn):
    def wrapper(*args, **kwargs):
        cls = args[0]
        splash = QtGui.QWidget (cls)
        splash.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        rect = cls.rect()
        # rect.setTop (rect.height()-50)
        splash.setGeometry(rect)
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Background, QtGui.QColor(0,0,0,150))
        splash.setAutoFillBackground(True)
        splash.setPalette(palette)
        splash.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
        splash.show()

        fn( *args, **kwargs)

        splash.destroy()
    return wrapper
@spinner
def action (w):
    time.sleep (10)

def main():
    app = QtGui.QApplication ( sys.argv )
    w = QtGui.QDialog ()
    l = QtGui.QHBoxLayout(w)
    b = QtGui.QPushButton ('Hello world')
    l.addWidget (b)
    b.clicked.connect(lambda :action(w))
    w.show()

    sys.exit( app.exec_() )

if __name__ == "__main__":
    main()

But the splash appear only at the end ... There is a work around ? Cheers

Upvotes: 0

Views: 1237

Answers (1)

Nejat
Nejat

Reputation: 32635

Qt has the QSplashScreen class which makes managing splash screens so simple :

splashPixmap = QPixmap('MyImage.png')
splash = QSplashScreen(splashPixmap, Qt.WindowStaysOnTopHint)
splash.setMask(splashPixmap.mask())
splash.show()
app.processEvents()

time.sleep(2)

...

This shows a splash screen containing an image with transparent parts and sleeps for 2 seconds.

Upvotes: 2

Related Questions