Custom splashscreen with text: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Cleanup)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
[[Category:snippets]]
Snippet how to create a splashscreen with opacity and dynamic text.


[[Category:snippets]]


= Custom splashscreen with text =
In main.cpp:


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


Example of main:
<code>
<code>QPixmap splashImage(":images/splash.png");
QPixmap splashImage(":images/splash.png");
QPixmap splashMask(":images/splashmask.png");
QPixmap splashMask(":images/splashmask.png");


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


Line 26: Line 25:
splash->show();
splash->show();


/''' To intercept mousclick to hide splash screen. Since the
/* To intercept mousclick to hide splash screen. Since the
splash screen is typically displayed before the event loop
splash screen is typically displayed before the event loop
has started running, it is necessary to periodically call. */
has started running, it is necessary to periodically call. */
Line 33: Line 32:
splash->showStatusMessage(QObject::tr("Initializing…"));
splash->showStatusMessage(QObject::tr("Initializing…"));


/''' Some code here */
/* Some code here */


app.processEvents();
app.processEvents();


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


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


Line 46: Line 50:
#include <QPainter>
#include <QPainter>


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


#endif // CUSTOMSPLASHSCREEN_H</code>
#endif // CUSTOMSPLASHSCREEN_H
</code>
 


customSplashScreen.cpp:
customSplashScreen.cpp:
<code>#include "customSplashScreen.h"


customSplashScreen::customSplashScreen(const QPixmap&amp;amp; pixmap)
 
<code>
#include "customSplashScreen.h"
 
customSplashScreen::customSplashScreen(const QPixmap& pixmap)  
{
{
QSplashScreen::setPixmap(pixmap);
  QSplashScreen::setPixmap(pixmap);
};
};


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


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


void customSplashScreen::setMessageRect(QRect rect, int alignement)
void customSplashScreen::setMessageRect(QRect rect, int alignement)
{
{
this->rect = rect;
    this->rect = rect;
this->alignement = alignement;
    this->alignement = alignement;
};</code>
};
 
</code>
Splash PNG:
[[Image:http://img.mobypicture.com/4ffeff271423757e1833f553a292bd1c_view.jpg|splash]]
 
Mask PNG:

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;
};