[PyQt] Delaying splash screen
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
Code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
"""
Splash doc
"""
messages = ["nothing"]
sAlignment = 0
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.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
msecs = 0
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
Code:
#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:
void clearMessage();
int sAlignment;
};
#endif // TSPLASHSCREEN_H
tesplashscreen.cpp
Code:
#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();
}
{
aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10);
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;
}
Re: [PyQt] Delaying splash screen
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):
Code:
TypeError object of type int has no len()
tesplashscreen.py
Code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
"""
Splash doc
"""
# messages = ["nothing"]
sAlignment = 0
def __init__(self, pixmap):
super
(QFrame, self
).__init__
() self.sPixmap = pixmap
self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)
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.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
msecs = 0
aSplashScreen = TeSplashScreen(self.sPixmap)
aSplashScreen.show
for i in range(len(messages)):
aSplashScreen.showMessage(messages[i], alignment, color)
SleeperThread.msleep(delay)
Re: [PyQt] Delaying splash screen