PDA

View Full Version : [PyQt] Delaying splash screen



M. Bashir
7th September 2011, 13:49
Hi guys,

I wrote a tiny class for delaying splash screen in C++ but when I tired to re-write in python it didn't work!

Could you please help me, I'm still a newbie in PyQt.

tesplashscreen.py

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class TeSplashScreen(QFrame):
"""
Splash doc
"""

app = QApplication(sys.argv)
sPixmap = QPixmap(10, 10)
sMessage = QString()
messages = ["nothing"]
sAlignment = 0
sColor = QColor()

def __init__(self, pixmap):
self.sPixmap = pixmap
self.QFrame(self, Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setFixedSize(sPixmap.size())

def clearMessage(self):
sMessage.clear()
repaint()

def showMessage(self, theMessage, theAlignment, theColor):
self.sMessage = theMessage
self.sAlignment = theAlignment
self.sColor = theColor
repaint()

def paintEvent(self, pe):
aTextRect(rect())
aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10)
aPainter = QPainter(self)
aPainter.drawPixmap(rect(), self.sPixmap)
aPainter.setPen(self.sColor)
aPainter.drawText(aTextRect, self.sAlignment, self.sMessage)

def showSplash(self, delay, messages, alignment, color):
delay = 0
self.messages = messages
alignment = 0
color = QColor(color)
class SleeperThread(QThread):
msecs = 0
QThread.msleep(msecs)
aSplashScreen = TeSplashScreen(self.sPixmap)
aSplashScreen.show
for i in range(len(messages)):
aSplashScreen.showMessage(messages[i], alignment, color)
SleeperThread.msleep(delay)

# def showSplash(delay, messages, alignment, color):
# delay = 0
# messages = QStringList()
# alignment = 0
# color = QColor()
# class SleeperThread(QThread):
# msecs = 0
# QThread.msleep(msecs)
# aSplashScreen = TeSplashScreen(sPixmap)
# aSplashScreen.show()
# for i in range(messages.count()):
# aSplashScreen.showMessage(messages[i], alignment, color)
# SleeperThread.msleep(delay)


tesplashscreen.h

#ifndef TSPLASHSCREEN_H
#define TSPLASHSCREEN_H

#include <QFrame>
#include <QPainter>
#include <QThread>

class TeSplashScreen : public QFrame
{
public:
TeSplashScreen(const QPixmap& pixmap);
void showMessage(const QString& theMessage, int theAlignment = Qt::AlignLeft, const QColor& theColor = Qt::black);
void showSplash(int delay, QStringList messages, int alignment = Qt::AlignLeft, const QColor& color = Qt::black);
void showSplash(QList<int> delaies, int defaultDelay, QStringList messages, int alignment = Qt::AlignLeft, const QColor& color = Qt::black);

private:
virtual void paintEvent(QPaintEvent* pe);
void clearMessage();
QPixmap sPixmap;
QString sMessage;
int sAlignment;
QColor sColor;
};

#endif // TSPLASHSCREEN_H


tesplashscreen.cpp

#include "tesplashscreen.h"

TeSplashScreen::TeSplashScreen(const QPixmap& thePixmap)
: QFrame(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint)
, sPixmap(thePixmap)
{
setAttribute(Qt::WA_TranslucentBackground);
setFixedSize(sPixmap.size());
};

void TeSplashScreen::clearMessage()
{
sMessage.clear();
repaint();
}

void TeSplashScreen::showMessage(const QString& theMessage, int theAlignment, const QColor& theColor)
{
sMessage = theMessage;
sAlignment = theAlignment;
sColor = theColor;
repaint();
}

void TeSplashScreen::paintEvent(QPaintEvent* pe)
{
QRect aTextRect(rect());
aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10);

QPainter aPainter(this);
aPainter.drawPixmap(rect(), sPixmap);
aPainter.setPen(sColor);
aPainter.drawText(aTextRect, sAlignment, sMessage);
}

void TeSplashScreen::showSplash(int delay, QStringList messages, int alignment, const QColor& color)
{
class SleeperThread : public QThread
{
public:
static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
};

TeSplashScreen* aSplashScreen = new TeSplashScreen(sPixmap);
aSplashScreen->show();

for(int i=0; i<messages.count();++i)
{
aSplashScreen->showMessage(messages[i], alignment, color);
SleeperThread::msleep(delay);
}

delete aSplashScreen;
}

void TeSplashScreen::showSplash(QList<int> delaies, int defaultDelay, QStringList messages, int alignment, const QColor& color)
{
class SleeperThread : public QThread
{
public:
static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
};

TeSplashScreen* aSplashScreen = new TeSplashScreen(sPixmap);
aSplashScreen->show();

for(int i=0; i<messages.count();++i)
{
aSplashScreen->showMessage(messages[i], alignment, color);
if(i<delaies.count())
SleeperThread::msleep(delaies[i]);
else
SleeperThread::msleep(defaultDelay);
}

delete aSplashScreen;
}

M. Bashir
8th September 2011, 00:53
I modified py class but I faced a new issue I think it's related to using list (I don't know how to use QStringList in PyQt):

TypeError object of type int has no len()

tesplashscreen.py

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class TeSplashScreen(QFrame):
"""
Splash doc
"""

app = QApplication(sys.argv)
sPixmap = QPixmap(10, 10)
sMessage = QString()
# messages = ["nothing"]
sAlignment = 0
sColor = QColor()

def __init__(self, pixmap):
super(QFrame, self).__init__()
self.sPixmap = pixmap
self.setWindowFlags(Qt.FramelessWindowHint|Qt.Wind owStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setFixedSize(self.sPixmap.size())

def clearMessage(self):
self.sMessage.clear()
self.repaint()

def showMessage(self, theMessage, theAlignment, theColor):
self.sMessage = theMessage
self.sAlignment = theAlignment
self.sColor = theColor
self.repaint()

def paintEvent(self, pe):
aTextRect = QRect(self.rect())
aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10)
aPainter = QPainter(self)
aPainter.drawPixmap(self.rect(), self.sPixmap)
aPainter.setPen(self.sColor)
aPainter.drawText(aTextRect, self.sAlignment, self.sMessage)

def showSplash(self, delay, messages, alignment, color):
delay = 0
# self.messages = messages
alignment = 0
color = QColor(color)
class SleeperThread(QThread):
msecs = 0
QThread.msleep(msecs)
aSplashScreen = TeSplashScreen(self.sPixmap)
aSplashScreen.show
for i in range(len(messages)):
aSplashScreen.showMessage(messages[i], alignment, color)
SleeperThread.msleep(delay)