Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

QSplashScreen-Replacement-for-Semitransparent-Images: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:snippets]][toc align_right="yes" depth="4"]
[[Category:snippets]][toc align_right="yes" depth="4"]


= CSplashScreen =
= CSplashScreen =
Line 11: Line 11:
==== CSplashScreen.pro ====
==== CSplashScreen.pro ====


<code><br />HEADERS ''= CSplashScreen.h<br />SOURCES''= CSplashScreen.cpp  main.cpp<br />OTHER_FILES ''=<br />RESOURCES''= images.qrc<br /></code>
<code>
HEADERS ''= CSplashScreen.h
SOURCES''= CSplashScreen.cpp  main.cpp
OTHER_FILES ''=
RESOURCES''= images.qrc
</code>


==== CSplashScreen.h ====
==== CSplashScreen.h ====


<code><br />#ifndef CSPLASHSCREEN_''H<br />#define CSPLASHSCREENH
<code>
<br />#include &lt;QFrame&amp;gt;
#ifndef CSPLASHSCREEN_''H
<br />////////////////////////////////////////////////////////////////////////////<br />class CSplashScreen : public QFrame<br />{<br />public:<br /> CSplashScreen(const QPixmap&amp;amp; pixmap);
#define CSPLASHSCREENH
<br /> void clearMessage();<br /> void showMessage(const QString&amp;amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&amp;amp; theColor = Qt::black);
<br />private:<br /> virtual void paintEvent(QPaintEvent* pe);
<br /> QPixmap itsPixmap;<br /> QString itsMessage;<br /> int itsAlignment;<br /> QColor itsColor;<br />};


<br />#endif // CSPLASHSCREEN''_H<br /></code>
#include <QFrame>
 
////////////////////////////////////////////////////////////////////////////
class CSplashScreen : public QFrame
{
public:
CSplashScreen(const QPixmap&amp;amp; pixmap);
 
void clearMessage();
void showMessage(const QString&amp;amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&amp;amp; theColor = Qt::black);
 
private:
virtual void paintEvent(QPaintEvent* pe);
 
QPixmap itsPixmap;
QString itsMessage;
int itsAlignment;
QColor itsColor;
};
 
 
#endif // CSPLASHSCREEN''_H
</code>


==== CSplashScreen.cpp ====
==== CSplashScreen.cpp ====


<code><br />#include &lt;QPainter&amp;gt;<br />#include &quot;CSplashScreen.h&amp;quot;
<code>
#include <QPainter>
#include "CSplashScreen.h"


////////////////////////////////////////////////////////////////////////////<br />CSplashScreen::CSplashScreen(const QPixmap&amp;amp; thePixmap)<br /> : QFrame(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint)<br /> , itsPixmap(thePixmap)<br />{<br /> setAttribute(Qt::WA_TranslucentBackground);<br /> setFixedSize(itsPixmap.size());<br />};
////////////////////////////////////////////////////////////////////////////
CSplashScreen::CSplashScreen(const QPixmap&amp;amp; thePixmap)
: QFrame(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint)
, itsPixmap(thePixmap)
{
setAttribute(Qt::WA_TranslucentBackground);
setFixedSize(itsPixmap.size());
};


////////////////////////////////////////////////////////////////////////////<br />void CSplashScreen::clearMessage()<br />{<br /> itsMessage.clear();<br /> repaint();<br />}
////////////////////////////////////////////////////////////////////////////
void CSplashScreen::clearMessage()
{
itsMessage.clear();
repaint();
}


////////////////////////////////////////////////////////////////////////////<br />void CSplashScreen::showMessage(const QString&amp;amp; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&amp;amp; theColor/* = Qt::black*/)<br />{<br /> itsMessage = theMessage;<br /> itsAlignment = theAlignment;<br /> itsColor = theColor;<br /> repaint();<br />}
////////////////////////////////////////////////////////////////////////////
void CSplashScreen::showMessage(const QString&amp;amp; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&amp;amp; theColor/* = Qt::black*/)
{
itsMessage = theMessage;
itsAlignment = theAlignment;
itsColor = theColor;
repaint();
}


////////////////////////////////////////////////////////////////////////////<br />void CSplashScreen::paintEvent(QPaintEvent* pe)<br />{<br /> QRect aTextRect(rect());<br /> aTextRect.setRect(aTextRect.x() + 5, aTextRect.y() + 5, aTextRect.width() - 10, aTextRect.height() - 10);
////////////////////////////////////////////////////////////////////////////
void CSplashScreen::paintEvent(QPaintEvent* pe)
{
QRect aTextRect(rect());
aTextRect.setRect(aTextRect.x() + 5, aTextRect.y() + 5, aTextRect.width() - 10, aTextRect.height() - 10);


QPainter aPainter(this);<br /> aPainter.drawPixmap(rect(), itsPixmap);<br /> aPainter.setPen(itsColor);<br /> aPainter.drawText(aTextRect, itsAlignment, itsMessage);<br />}<br /></code>
QPainter aPainter(this);
aPainter.drawPixmap(rect(), itsPixmap);
aPainter.setPen(itsColor);
aPainter.drawText(aTextRect, itsAlignment, itsMessage);
}
</code>


==== main.cpp ====
==== main.cpp ====


<code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QThread&amp;gt;<br />#include &quot;CSplashSCreen.h&amp;quot;
<code>
#include <QApplication>
#include <QThread>
#include "CSplashSCreen.h"
 
int main(int argc, char '''argv[])
{
QApplication anApplication(argc, argv);
 
class SleeperThread : public QThread
{
public:
static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
};
 
QPixmap aSplashImage(":/splash.png");


int main(int argc, char '''argv[])<br />{<br /> QApplication anApplication(argc, argv);
CSplashScreen''' aSplashScreen = new CSplashScreen(aSplashImage);
<br /> class SleeperThread : public QThread<br /> {<br /> public:<br /> static void msleep(unsigned long msecs) {QThread::msleep(msecs);}<br /> };
aSplashScreen->show();
<br /> QPixmap aSplashImage(&quot;:/splash.png&amp;quot;);
<br /> CSplashScreen''' aSplashScreen = new CSplashScreen(aSplashImage);<br /> aSplashScreen-&gt;show();


for (int i = 1; i &lt;= 5; i++)<br /> {<br /> aSplashScreen-&gt;showMessage(QString(&quot;Processing %1…&quot;).arg(i), Qt::AlignTop | Qt::AlignLeft, Qt::white);<br /> SleeperThread::msleep(1000);<br /> }
for (int i = 1; i <= 5; i++)
{
aSplashScreen->showMessage(QString("Processing %1…").arg(i), Qt::AlignTop | Qt::AlignLeft, Qt::white);
SleeperThread::msleep(1000);
}


delete aSplashScreen;
delete aSplashScreen;


return 0;<br />}<br /></code>
return 0;
}
</code>


==== Example ====
==== Example ====


[[Image:http://img228.imageshack.us/img228/4619/csplashscreen.png|http://img228.imageshack.us/img228/4619/csplashscreen.png]]
[[Image:http://img228.imageshack.us/img228/4619/csplashscreen.png|http://img228.imageshack.us/img228/4619/csplashscreen.png]]

Revision as of 10:30, 25 February 2015

[toc align_right="yes" depth="4"]

CSplashScreen

The CSplashScreen class can be used as a replacement for the Qt class QSplashScreen if semitransparent images using an alpha channel with more than 1 bit needs to be used.

The new class tries to mimic as much as possible the functionality and appearance of the original QSplashScreen class and only misses a few methods to keep this code snipped as simple as possible.

In the implementation all comes down to use a QFrame with the Qt::WA_TranslucentBackground attribute and emulate the functionality of QSplashScreen.

CSplashScreen.pro

HEADERS ''= CSplashScreen.h
SOURCES''= CSplashScreen.cpp  main.cpp
OTHER_FILES ''=
RESOURCES''= images.qrc

CSplashScreen.h

#ifndef CSPLASHSCREEN_''H
#define CSPLASHSCREENH

#include <QFrame>

////////////////////////////////////////////////////////////////////////////
class CSplashScreen : public QFrame
{
public:
 CSplashScreen(const QPixmap&amp;amp; pixmap);

 void clearMessage();
 void showMessage(const QString&amp;amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&amp;amp; theColor = Qt::black);

private:
 virtual void paintEvent(QPaintEvent* pe);

 QPixmap itsPixmap;
 QString itsMessage;
 int itsAlignment;
 QColor itsColor;
};


#endif // CSPLASHSCREEN''_H

CSplashScreen.cpp

#include <QPainter>
#include "CSplashScreen.h"

////////////////////////////////////////////////////////////////////////////
CSplashScreen::CSplashScreen(const QPixmap&amp;amp; thePixmap)
 : QFrame(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint)
 , itsPixmap(thePixmap)
{
 setAttribute(Qt::WA_TranslucentBackground);
 setFixedSize(itsPixmap.size());
};

////////////////////////////////////////////////////////////////////////////
void CSplashScreen::clearMessage()
{
 itsMessage.clear();
 repaint();
}

////////////////////////////////////////////////////////////////////////////
void CSplashScreen::showMessage(const QString&amp;amp; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&amp;amp; theColor/* = Qt::black*/)
{
 itsMessage = theMessage;
 itsAlignment = theAlignment;
 itsColor = theColor;
 repaint();
}

////////////////////////////////////////////////////////////////////////////
void CSplashScreen::paintEvent(QPaintEvent* pe)
{
 QRect aTextRect(rect());
 aTextRect.setRect(aTextRect.x() + 5, aTextRect.y() + 5, aTextRect.width() - 10, aTextRect.height() - 10);

QPainter aPainter(this);
 aPainter.drawPixmap(rect(), itsPixmap);
 aPainter.setPen(itsColor);
 aPainter.drawText(aTextRect, itsAlignment, itsMessage);
}

main.cpp

#include <QApplication>
#include <QThread>
#include "CSplashSCreen.h"

int main(int argc, char '''argv[])
{
 QApplication anApplication(argc, argv);

 class SleeperThread : public QThread
 {
 public:
 static void msleep(unsigned long msecs) {QThread::msleep(msecs);}
 };

 QPixmap aSplashImage(":/splash.png");

 CSplashScreen''' aSplashScreen = new CSplashScreen(aSplashImage);
 aSplashScreen->show();

for (int i = 1; i <= 5; i++)
 {
 aSplashScreen->showMessage(QString("Processing %1…").arg(i), Qt::AlignTop | Qt::AlignLeft, Qt::white);
 SleeperThread::msleep(1000);
 }

delete aSplashScreen;

return 0;
}

Example

http://img228.imageshack.us/img228/4619/csplashscreen.png