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
Qt Code:
  1. from PyQt4.QtCore import *
  2. from PyQt4.QtGui import *
  3. import sys
  4. class TeSplashScreen(QFrame):
  5. """
  6. Splash doc
  7. """
  8.  
  9. app = QApplication(sys.argv)
  10. sPixmap = QPixmap(10, 10)
  11. sMessage = QString()
  12. messages = ["nothing"]
  13. sAlignment = 0
  14. sColor = QColor()
  15.  
  16. def __init__(self, pixmap):
  17. self.sPixmap = pixmap
  18. self.QFrame(self, Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)
  19. self.setAttribute(Qt.WA_TranslucentBackground)
  20. self.setFixedSize(sPixmap.size())
  21.  
  22. def clearMessage(self):
  23. sMessage.clear()
  24. repaint()
  25.  
  26. def showMessage(self, theMessage, theAlignment, theColor):
  27. self.sMessage = theMessage
  28. self.sAlignment = theAlignment
  29. self.sColor = theColor
  30. repaint()
  31.  
  32. def paintEvent(self, pe):
  33. aTextRect(rect())
  34. aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10)
  35. aPainter = QPainter(self)
  36. aPainter.drawPixmap(rect(), self.sPixmap)
  37. aPainter.setPen(self.sColor)
  38. aPainter.drawText(aTextRect, self.sAlignment, self.sMessage)
  39.  
  40. def showSplash(self, delay, messages, alignment, color):
  41. delay = 0
  42. self.messages = messages
  43. alignment = 0
  44. color = QColor(color)
  45. class SleeperThread(QThread):
  46. msecs = 0
  47. QThread.msleep(msecs)
  48. aSplashScreen = TeSplashScreen(self.sPixmap)
  49. aSplashScreen.show
  50. for i in range(len(messages)):
  51. aSplashScreen.showMessage(messages[i], alignment, color)
  52. SleeperThread.msleep(delay)
  53.  
  54. # def showSplash(delay, messages, alignment, color):
  55. # delay = 0
  56. # messages = QStringList()
  57. # alignment = 0
  58. # color = QColor()
  59. # class SleeperThread(QThread):
  60. # msecs = 0
  61. # QThread.msleep(msecs)
  62. # aSplashScreen = TeSplashScreen(sPixmap)
  63. # aSplashScreen.show()
  64. # for i in range(messages.count()):
  65. # aSplashScreen.showMessage(messages[i], alignment, color)
  66. # SleeperThread.msleep(delay)
To copy to clipboard, switch view to plain text mode 

tesplashscreen.h
Qt Code:
  1. #ifndef TSPLASHSCREEN_H
  2. #define TSPLASHSCREEN_H
  3.  
  4. #include <QFrame>
  5. #include <QPainter>
  6. #include <QThread>
  7.  
  8. class TeSplashScreen : public QFrame
  9. {
  10. public:
  11. TeSplashScreen(const QPixmap& pixmap);
  12. void showMessage(const QString& theMessage, int theAlignment = Qt::AlignLeft, const QColor& theColor = Qt::black);
  13. void showSplash(int delay, QStringList messages, int alignment = Qt::AlignLeft, const QColor& color = Qt::black);
  14. void showSplash(QList<int> delaies, int defaultDelay, QStringList messages, int alignment = Qt::AlignLeft, const QColor& color = Qt::black);
  15.  
  16. private:
  17. virtual void paintEvent(QPaintEvent* pe);
  18. void clearMessage();
  19. QPixmap sPixmap;
  20. QString sMessage;
  21. int sAlignment;
  22. QColor sColor;
  23. };
  24.  
  25. #endif // TSPLASHSCREEN_H
To copy to clipboard, switch view to plain text mode 

tesplashscreen.cpp
Qt Code:
  1. #include "tesplashscreen.h"
  2.  
  3. TeSplashScreen::TeSplashScreen(const QPixmap& thePixmap)
  4. : QFrame(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint)
  5. , sPixmap(thePixmap)
  6. {
  7. setAttribute(Qt::WA_TranslucentBackground);
  8. setFixedSize(sPixmap.size());
  9. };
  10.  
  11. void TeSplashScreen::clearMessage()
  12. {
  13. sMessage.clear();
  14. repaint();
  15. }
  16.  
  17. void TeSplashScreen::showMessage(const QString& theMessage, int theAlignment, const QColor& theColor)
  18. {
  19. sMessage = theMessage;
  20. sAlignment = theAlignment;
  21. sColor = theColor;
  22. repaint();
  23. }
  24.  
  25. void TeSplashScreen::paintEvent(QPaintEvent* pe)
  26. {
  27. QRect aTextRect(rect());
  28. aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5, aTextRect.width()-10, aTextRect.height()-10);
  29.  
  30. QPainter aPainter(this);
  31. aPainter.drawPixmap(rect(), sPixmap);
  32. aPainter.setPen(sColor);
  33. aPainter.drawText(aTextRect, sAlignment, sMessage);
  34. }
  35.  
  36. void TeSplashScreen::showSplash(int delay, QStringList messages, int alignment, const QColor& color)
  37. {
  38. class SleeperThread : public QThread
  39. {
  40. public:
  41. static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
  42. };
  43.  
  44. TeSplashScreen* aSplashScreen = new TeSplashScreen(sPixmap);
  45. aSplashScreen->show();
  46.  
  47. for(int i=0; i<messages.count();++i)
  48. {
  49. aSplashScreen->showMessage(messages[i], alignment, color);
  50. SleeperThread::msleep(delay);
  51. }
  52.  
  53. delete aSplashScreen;
  54. }
  55.  
  56. void TeSplashScreen::showSplash(QList<int> delaies, int defaultDelay, QStringList messages, int alignment, const QColor& color)
  57. {
  58. class SleeperThread : public QThread
  59. {
  60. public:
  61. static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
  62. };
  63.  
  64. TeSplashScreen* aSplashScreen = new TeSplashScreen(sPixmap);
  65. aSplashScreen->show();
  66.  
  67. for(int i=0; i<messages.count();++i)
  68. {
  69. aSplashScreen->showMessage(messages[i], alignment, color);
  70. if(i<delaies.count())
  71. SleeperThread::msleep(delaies[i]);
  72. else
  73. SleeperThread::msleep(defaultDelay);
  74. }
  75.  
  76. delete aSplashScreen;
  77. }
To copy to clipboard, switch view to plain text mode