Custom splashscreen with text: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]] | |||
= Custom splashscreen with text = | |||
Snippet how to create a splashscreen with opacity and dynamic text:<br />[[Image:http://img.mobypicture.com/aa470873918e7804b36e6fb037e5b0b0_view.jpg|screencap]] | |||
Example of main:<br /><code>QPixmap splashImage(":images/splash.png&quot;);<br />QPixmap splashMask(":images/splashmask.png&quot;); | |||
customSplashScreen. | 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…"));</code> | |||
customSplashScreen.h:<br /><code>#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</code> | |||
customSplashScreen.cpp:<br /><code>#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 />};</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(":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: