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:
=CSplashScreen=
[[Category:snippets]][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 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.
Line 7: Line 9:
In the implementation all comes down to use a QFrame with the Qt::WA_TranslucentBackground attribute and emulate the functionality of QSplashScreen.
In the implementation all comes down to use a QFrame with the Qt::WA_TranslucentBackground attribute and emulate the functionality of QSplashScreen.


====CSplashScreen.pro====
==== CSplashScreen.pro ====
 
<code><br />HEADERS ''= CSplashScreen.h<br />SOURCES''= CSplashScreen.cpp  main.cpp<br />OTHER_FILES ''=<br />RESOURCES''= images.qrc<br /></code>
 
==== CSplashScreen.h ====
 
<code><br />#ifndef CSPLASHSCREEN_''H<br />#define CSPLASHSCREENH
<br />#include &lt;QFrame&amp;gt;
<br />////////////////////////////////////////////////////////////////////////////<br />class CSplashScreen : public QFrame<br />{<br />public:<br /> CSplashScreen(const QPixmap&amp;amp; pixmap);
<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>
 
==== CSplashScreen.cpp ====
 
<code><br />#include &lt;QPainter&amp;gt;<br />#include &quot;CSplashScreen.h&amp;quot;
 
////////////////////////////////////////////////////////////////////////////<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 />};
 
////////////////////////////////////////////////////////////////////////////<br />void CSplashScreen::clearMessage()<br />{<br /> itsMessage.clear();<br /> repaint();<br />}
 
////////////////////////////////////////////////////////////////////////////<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 />}
 
////////////////////////////////////////////////////////////////////////////<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);
 
QPainter aPainter(this);<br /> aPainter.drawPixmap(rect(), itsPixmap);<br /> aPainter.setPen(itsColor);<br /> aPainter.drawText(aTextRect, itsAlignment, itsMessage);<br />}<br /></code>
 
==== main.cpp ====


====CSplashScreen.h====
<code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QThread&amp;gt;<br />#include &quot;CSplashSCreen.h&amp;quot;


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


====main.cpp====
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 /> }


====Example====
delete aSplashScreen;


[[Image:csplashscreen.png]]
return 0;<br />}<br /></code>


===Categories:===
==== Example ====


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

Revision as of 10:10, 24 February 2015

[toc align_right="yes&quot; depth="4&quot;]

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

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

CSplashScreen.h

<br />#ifndef CSPLASHSCREEN_''H<br />#define CSPLASHSCREENH
<br />#include &lt;QFrame&amp;gt;
<br />////////////////////////////////////////////////////////////////////////////<br />class CSplashScreen : public QFrame<br />{<br />public:<br /> CSplashScreen(const QPixmap&amp;amp; pixmap);
<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 />

CSplashScreen.cpp

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

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

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

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

////////////////////////////////////////////////////////////////////////////<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);

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

main.cpp

<br />#include &lt;QApplication&amp;gt;<br />#include &lt;QThread&amp;gt;<br />#include &quot;CSplashSCreen.h&amp;quot;

int main(int argc, char '''argv[])<br />{<br /> QApplication anApplication(argc, argv);
<br /> class SleeperThread : public QThread<br /> {<br /> public:<br /> static void msleep(unsigned long msecs) {QThread::msleep(msecs);}<br /> };
<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 /> }

delete aSplashScreen;

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

Example

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