Custom splashscreen with text
Jump to navigation
Jump to search
Custom splashscreen with text
Snippet how to create a splashscreen with opacity and dynamic text:
screencap
Example of main:
QPixmap splashImage(":images/splash.png&quot;);<br />QPixmap splashMask(":images/splashmask.png&quot;);
customSplashScreen '''splash = new customSplashScreen(splashImage);<br />splash->setMessageRect(QRect::QRect(7, 253, 415, 14), Qt::AlignCenter); // Setting the message position.
<br />QFont splashFont;<br />splashFont.setFamily("Arial&quot;);<br />splashFont.setBold(true);<br />splashFont.setPixelSize(9);<br />splashFont.setStretch(125);
<br />splash->setFont(splashFont);<br />splash->setMask(splashMask);<br />splash->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);<br />splash->show();
<br />/''' To intercept mousclick to hide splash screen. Since the<br />splash screen is typically displayed before the event loop<br />has started running, it is necessary to periodically call. '''/<br />app.processEvents();
<br />splash->showStatusMessage(QObject::tr("Initializing…"));
<br />/''' Some code here */
app.processEvents();
splash->showStatusMessage(QObject::tr("Loading something…"));
customSplashScreen.h:
#ifndef CUSTOMSPLASHSCREEN_H<br />#define CUSTOMSPLASHSCREEN_H
#include <QSplashScreen&gt;<br />#include <QPainter&gt;
class customSplashScreen<br /> :public QSplashScreen<br />{
public:<br /> customSplashScreen(const QPixmap&amp; pixmap);<br /> ~customSplashScreen();<br /> virtual void drawContents(QPainter *painter);<br /> void showStatusMessage(const QString &message, const QColor &color = Qt::black);<br /> void setMessageRect(QRect rect, int alignment = Qt::AlignLeft);
private:<br /> QString message;<br /> int alignement;<br /> QColor color;<br /> QRect rect;<br />};
#endif // CUSTOMSPLASHSCREEN_H
customSplashScreen.cpp:
#include "customSplashScreen.h&quot;
customSplashScreen::customSplashScreen(const QPixmap&amp; pixmap)<br />{<br /> QSplashScreen::setPixmap(pixmap);<br />};
customSplashScreen::~customSplashScreen()<br />{<br />};
void customSplashScreen::drawContents(QPainter *painter)<br />{<br /> QPixmap textPix = QSplashScreen::pixmap();<br /> painter->setPen(this->color);<br /> painter->drawText(this->rect, this->alignement, this->message);<br />};
void customSplashScreen::showStatusMessage(const QString &message, const QColor &color)<br />{<br /> this->message = message;<br /> this->color = color;<br /> this->showMessage(this->message, this->alignement, this->color);<br />};
void customSplashScreen::setMessageRect(QRect rect, int alignement)<br />{<br /> this->rect = rect;<br /> this->alignement = alignement;<br />};
Splash PNG:
splash
Mask PNG: