Custom splashscreen with text: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Custom splashscreen with text=
[[Category:snippets]]


Snippet how to create a splashscreen with opacity and dynamic text:<br />[[Image:aa470873918e7804b36e6fb037e5b0b0_view.jpg|screencap]]
= Custom splashscreen with text =


Example of main:<br />
Snippet how to create a splashscreen with opacity and dynamic text:<br />[[Image:http://img.mobypicture.com/aa470873918e7804b36e6fb037e5b0b0_view.jpg|screencap]]


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


customSplashScreen.cpp:<br />
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 */


Splash <span class="caps">PNG</span>:<br />[[Image:4ffeff271423757e1833f553a292bd1c_view.jpg|splash]]
app.processEvents();


Mask <span class="caps">PNG</span>:<br />[[Image:158962e6c51d76d0bfe042338fb27d6e_view.jpg|mask]]
splash-&gt;showStatusMessage(QObject::tr(&quot;Loading something…&quot;));</code>


===Categories:===
customSplashScreen.h:<br /><code>#ifndef CUSTOMSPLASHSCREEN_H<br />#define CUSTOMSPLASHSCREEN_H


* [[:Category:snippets|snippets]]
#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</code>
 
customSplashScreen.cpp:<br /><code>#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 />};</code>
 
Splash PNG:<br />[[Image:http://img.mobypicture.com/4ffeff271423757e1833f553a292bd1c_view.jpg|splash]]
 
Mask PNG:

Revision as of 09:40, 24 February 2015


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: