Custom splashscreen with text: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{LangSwitch}}
[[Category:snippets]]
[[Category:snippets]]
Snippet how to create a splashscreen with opacity and dynamic text.


= 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]]
In main.cpp:


Example of main:<br /><code>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.
<code>
<br />QFont splashFont;<br />splashFont.setFamily(&quot;Arial&amp;quot;);<br />splashFont.setBold(true);<br />splashFont.setPixelSize(9);<br />splashFont.setStretch(125);
QPixmap splashImage(":images/splash.png");
<br />splash-&gt;setFont(splashFont);<br />splash-&gt;setMask(splashMask);<br />splash-&gt;setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);<br />splash-&gt;show();
QPixmap splashMask(":images/splashmask.png");
<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 */


customSplashScreen* splash = new customSplashScreen(splashImage);
splash->setMessageRect(QRect::QRect(7, 253, 415, 14), Qt::AlignCenter); // Setting the message position.
QFont splashFont;
splashFont.setFamily("Arial");
splashFont.setBold(true);
splashFont.setPixelSize(9);
splashFont.setStretch(125);
splash->setFont(splashFont);
splash->setMask(splashMask);
splash->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
splash->show();
/* To intercept mousclick to hide splash screen. Since the
splash screen is typically displayed before the event loop
has started running, it is necessary to periodically call. */
app.processEvents();
app.processEvents();


splash-&gt;showStatusMessage(QObject::tr(&quot;Loading something…&quot;));</code>
splash->showStatusMessage(QObject::tr("Initializing…"));
 
/* Some code here */
 
app.processEvents();
 
splash->showStatusMessage(QObject::tr("Loading something…"));
</code>
 
 
customSplashScreen.h:


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


#include &lt;QSplashScreen&amp;gt;<br />#include &lt;QPainter&amp;gt;
<code>
#ifndef CUSTOMSPLASHSCREEN_H
#define CUSTOMSPLASHSCREEN_H


class customSplashScreen<br /> :public QSplashScreen<br />{
#include <QSplashScreen>
#include <QPainter>


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);
class customSplashScreen : public QSplashScreen
{
public:
    customSplashScreen(const QPixmap& pixmap);
    ~customSplashScreen();
    virtual void drawContents(QPainter *painter);
    void showStatusMessage(const QString &message, const QColor &color = Qt::black);
    void setMessageRect(QRect rect, int alignment = Qt::AlignLeft);
private:
    QString message;
    int alignement;
    QColor color;
    QRect rect;
};


private:<br /> QString message;<br /> int alignement;<br /> QColor color;<br /> QRect rect;<br />};
#endif // CUSTOMSPLASHSCREEN_H
</code>


#endif // CUSTOMSPLASHSCREEN_H</code>


customSplashScreen.cpp:<br /><code>#include &quot;customSplashScreen.h&amp;quot;
customSplashScreen.cpp:


customSplashScreen::customSplashScreen(const QPixmap&amp;amp; pixmap)<br />{<br /> QSplashScreen::setPixmap(pixmap);<br />};


customSplashScreen::~customSplashScreen()<br />{<br />};
<code>
#include "customSplashScreen.h"


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 />};
customSplashScreen::customSplashScreen(const QPixmap& pixmap)  
{
  QSplashScreen::setPixmap(pixmap);
};


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 />};
customSplashScreen::~customSplashScreen()
{
};


void customSplashScreen::setMessageRect(QRect rect, int alignement)<br />{<br /> this-&gt;rect = rect;<br /> this-&gt;alignement = alignement;<br />};</code>
void customSplashScreen::drawContents(QPainter *painter)
{
    QPixmap textPix = QSplashScreen::pixmap();
    painter->setPen(this->color);
    painter->drawText(this->rect, this->alignement, this->message);
};


Splash PNG:<br />[[Image:http://img.mobypicture.com/4ffeff271423757e1833f553a292bd1c_view.jpg|splash]]
void customSplashScreen::showStatusMessage(const QString &message, const QColor &color)
{
    this->message = message;
    this->color = color;
    this->showMessage(this->message, this->alignement, this->color);
};


Mask PNG:
void customSplashScreen::setMessageRect(QRect rect, int alignement)
{
    this->rect = rect;
    this->alignement = alignement;
};
</code>

Latest revision as of 13:26, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Snippet how to create a splashscreen with opacity and dynamic text.


In main.cpp:


QPixmap splashImage(":images/splash.png");
QPixmap splashMask(":images/splashmask.png");

customSplashScreen* splash = new customSplashScreen(splashImage);
splash->setMessageRect(QRect::QRect(7, 253, 415, 14), Qt::AlignCenter); // Setting the message position.

QFont splashFont;
splashFont.setFamily("Arial");
splashFont.setBold(true);
splashFont.setPixelSize(9);
splashFont.setStretch(125);

splash->setFont(splashFont);
splash->setMask(splashMask);
splash->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
splash->show();

/* To intercept mousclick to hide splash screen. Since the
splash screen is typically displayed before the event loop
has started running, it is necessary to periodically call. */
app.processEvents();

splash->showStatusMessage(QObject::tr("Initializing…"));

/* Some code here */

app.processEvents();

splash->showStatusMessage(QObject::tr("Loading something…"));


customSplashScreen.h:


#ifndef CUSTOMSPLASHSCREEN_H
#define CUSTOMSPLASHSCREEN_H

#include <QSplashScreen>
#include <QPainter>

class customSplashScreen : public QSplashScreen
{
public:
    customSplashScreen(const QPixmap& pixmap);
    ~customSplashScreen();
    virtual void drawContents(QPainter *painter);
    void showStatusMessage(const QString &message, const QColor &color = Qt::black);
    void setMessageRect(QRect rect, int alignment = Qt::AlignLeft);
private:
    QString message;
    int alignement;
    QColor color;
    QRect rect;
};

#endif // CUSTOMSPLASHSCREEN_H


customSplashScreen.cpp:


#include "customSplashScreen.h"

customSplashScreen::customSplashScreen(const QPixmap& pixmap) 
{
   QSplashScreen::setPixmap(pixmap);
};

customSplashScreen::~customSplashScreen()
{
};

void customSplashScreen::drawContents(QPainter *painter)
{
    QPixmap textPix = QSplashScreen::pixmap();
    painter->setPen(this->color);
    painter->drawText(this->rect, this->alignement, this->message);
};

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

void customSplashScreen::setMessageRect(QRect rect, int alignement)
{
    this->rect = rect;
    this->alignement = alignement;
};