Custom splashscreen with text

Snippet how to create a splashscreen with opacity and dynamic text:
screencap

Example of main:

  1. QPixmap splashImage(":images/splash.png");
  2. QPixmap splashMask(":images/splashmask.png");
  3.  
  4. customSplashScreen *splash = new customSplashScreen(splashImage);
  5. splash->setMessageRect(QRect::QRect(7, 253, 415, 14), Qt::AlignCenter); // Setting the message position.
  6.  
  7. QFont splashFont;
  8. splashFont.setFamily("Arial");
  9. splashFont.setBold(true);
  10. splashFont.setPixelSize(9);
  11. splashFont.setStretch(125);
  12.  
  13. splash->setFont(splashFont);
  14. splash->setMask(splashMask);
  15. splash->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
  16. splash->show();
  17.  
  18. /* To intercept mousclick to hide splash screen. Since the
  19. splash screen is typically displayed before the event loop
  20. has started running, it is necessary to periodically call. */
  21. app.processEvents();
  22.  
  23. splash->showStatusMessage(QObject::tr("Initializing..."));
  24.  
  25. /* Some code here */
  26.  
  27. app.processEvents();
  28.  
  29. splash->showStatusMessage(QObject::tr("Loading something..."));

customSplashScreen.h:

  1. #ifndef CUSTOMSPLASHSCREEN_H
  2. #define CUSTOMSPLASHSCREEN_H
  3.  
  4. #include <QSplashScreen>
  5. #include <QPainter>
  6.  
  7. class customSplashScreen
  8.  :public QSplashScreen
  9. {
  10.  
  11. public:
  12.  customSplashScreen(const QPixmap& pixmap);
  13.  ~customSplashScreen();
  14.  virtual void drawContents(QPainter *painter);
  15.  void showStatusMessage(const QString &message, const QColor &color = Qt::black);
  16.  void setMessageRect(QRect rect, int alignment = Qt::AlignLeft);
  17.  
  18. private:
  19.  QString message;
  20.  int alignement;
  21.  QColor color;
  22.  QRect rect;
  23. };
  24.  
  25. #endif // CUSTOMSPLASHSCREEN_H

customSplashScreen.cpp:

  1. #include "customSplashScreen.h"
  2.  
  3. customSplashScreen::customSplashScreen(const QPixmap& pixmap)
  4. {
  5.  QSplashScreen::setPixmap(pixmap);
  6. };
  7.  
  8. customSplashScreen::~customSplashScreen()
  9. {
  10. };
  11.  
  12. void customSplashScreen::drawContents(QPainter *painter)
  13. {
  14.  QPixmap textPix = QSplashScreen::pixmap();
  15.  painter->setPen(this->color);
  16.  painter->drawText(this->rect, this->alignement, this->message);
  17. };
  18.  
  19. void customSplashScreen::showStatusMessage(const QString &message, const QColor &color)
  20. {
  21.  this->message = message;
  22.  this->color = color;
  23.  this->showMessage(this->message, this->alignement, this->color);
  24. };
  25.  
  26. void customSplashScreen::setMessageRect(QRect rect, int alignement)
  27. {
  28.  this->rect = rect;
  29.  this->alignement = alignement;
  30. };

Splash PNG:
splash

Mask PNG:
mask

Categories: