Custom splashscreen with text

From Qt Wiki
Revision as of 09:40, 24 February 2015 by Maintenance script (talk | contribs)

Jump to: navigation, search


Custom splashscreen with text

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

Example of main:
QPixmap splashImage(&quot;:images/splash.png&amp;quot;);<br />QPixmap splashMask(&quot;:images/splashmask.png&amp;quot;);

customSplashScreen '''splash = new customSplashScreen(splashImage);<br />splash-&gt;setMessageRect(QRect::QRect(7, 253, 415, 14), Qt::AlignCenter); // Setting the message position.
<br />QFont splashFont;<br />splashFont.setFamily(&quot;Arial&amp;quot;);<br />splashFont.setBold(true);<br />splashFont.setPixelSize(9);<br />splashFont.setStretch(125);
<br />splash-&gt;setFont(splashFont);<br />splash-&gt;setMask(splashMask);<br />splash-&gt;setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);<br />splash-&gt;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-&gt;showStatusMessage(QObject::tr(&quot;Initializing&quot;));
<br />/''' Some code here */

app.processEvents();

splash-&gt;showStatusMessage(QObject::tr(&quot;Loading something&quot;));
customSplashScreen.h:
#ifndef CUSTOMSPLASHSCREEN_H<br />#define CUSTOMSPLASHSCREEN_H

#include &lt;QSplashScreen&amp;gt;<br />#include &lt;QPainter&amp;gt;

class customSplashScreen<br /> :public QSplashScreen<br />{

public:<br /> customSplashScreen(const QPixmap&amp;amp; pixmap);<br /> ~customSplashScreen();<br /> virtual void drawContents(QPainter *painter);<br /> void showStatusMessage(const QString &amp;message, const QColor &amp;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 &quot;customSplashScreen.h&amp;quot;

customSplashScreen::customSplashScreen(const QPixmap&amp;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-&gt;setPen(this-&gt;color);<br /> painter-&gt;drawText(this-&gt;rect, this-&gt;alignement, this-&gt;message);<br />};

void customSplashScreen::showStatusMessage(const QString &amp;message, const QColor &amp;color)<br />{<br /> this-&gt;message = message;<br /> this-&gt;color = color;<br /> this-&gt;showMessage(this-&gt;message, this-&gt;alignement, this-&gt;color);<br />};

void customSplashScreen::setMessageRect(QRect rect, int alignement)<br />{<br /> this-&gt;rect = rect;<br /> this-&gt;alignement = alignement;<br />};

Splash PNG:
splash

Mask PNG: