QSplashScreen-Replacement-for-Semitransparent-Images: Difference between revisions
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 <QFrame&gt; | |||
<br />////////////////////////////////////////////////////////////////////////////<br />class CSplashScreen : public QFrame<br />{<br />public:<br /> CSplashScreen(const QPixmap&amp; pixmap); | |||
<br /> void clearMessage();<br /> void showMessage(const QString&amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&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 <QPainter&gt;<br />#include "CSplashScreen.h&quot; | |||
////////////////////////////////////////////////////////////////////////////<br />CSplashScreen::CSplashScreen(const QPixmap&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; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&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 ==== | |||
<code><br />#include <QApplication&gt;<br />#include <QThread&gt;<br />#include "CSplashSCreen.h&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(":/splash.png&quot;); | |||
<br /> CSplashScreen''' aSplashScreen = new CSplashScreen(aSplashImage);<br /> aSplashScreen->show(); | |||
== | for (int i = 1; i <= 5; i++)<br /> {<br /> aSplashScreen->showMessage(QString("Processing %1…").arg(i), Qt::AlignTop | Qt::AlignLeft, Qt::white);<br /> SleeperThread::msleep(1000);<br /> } | ||
delete aSplashScreen; | |||
return 0;<br />}<br /></code> | |||
=== | ==== Example ==== | ||
[[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" 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
<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 <QFrame&gt;
<br />////////////////////////////////////////////////////////////////////////////<br />class CSplashScreen : public QFrame<br />{<br />public:<br /> CSplashScreen(const QPixmap&amp; pixmap);
<br /> void clearMessage();<br /> void showMessage(const QString&amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&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 <QPainter&gt;<br />#include "CSplashScreen.h&quot;
////////////////////////////////////////////////////////////////////////////<br />CSplashScreen::CSplashScreen(const QPixmap&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; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&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 <QApplication&gt;<br />#include <QThread&gt;<br />#include "CSplashSCreen.h&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(":/splash.png&quot;);
<br /> CSplashScreen''' aSplashScreen = new CSplashScreen(aSplashImage);<br /> aSplashScreen->show();
for (int i = 1; i <= 5; i++)<br /> {<br /> aSplashScreen->showMessage(QString("Processing %1…").arg(i), Qt::AlignTop | Qt::AlignLeft, Qt::white);<br /> SleeperThread::msleep(1000);<br /> }
delete aSplashScreen;
return 0;<br />}<br />