QSplashScreen-Replacement-for-Semitransparent-Images: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]][toc align_right= | [[Category:snippets]][toc align_right="yes" depth="4"] | ||
= CSplashScreen = | = CSplashScreen = | ||
Line 11: | Line 11: | ||
==== CSplashScreen.pro ==== | ==== CSplashScreen.pro ==== | ||
<code> | <code> | ||
HEADERS ''= CSplashScreen.h | |||
SOURCES''= CSplashScreen.cpp main.cpp | |||
OTHER_FILES ''= | |||
RESOURCES''= images.qrc | |||
</code> | |||
==== CSplashScreen.h ==== | ==== CSplashScreen.h ==== | ||
<code> | <code> | ||
#ifndef CSPLASHSCREEN_''H | |||
#define CSPLASHSCREENH | |||
< | #include <QFrame> | ||
//////////////////////////////////////////////////////////////////////////// | |||
class CSplashScreen : public QFrame | |||
{ | |||
public: | |||
CSplashScreen(const QPixmap&amp; pixmap); | |||
void clearMessage(); | |||
void showMessage(const QString&amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&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> | <code> | ||
#include <QPainter> | |||
#include "CSplashScreen.h" | |||
//////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////// | ||
CSplashScreen::CSplashScreen(const QPixmap&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; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&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); | QPainter aPainter(this); | ||
aPainter.drawPixmap(rect(), itsPixmap); | |||
aPainter.setPen(itsColor); | |||
aPainter.drawText(aTextRect, itsAlignment, itsMessage); | |||
} | |||
</code> | |||
==== main.cpp ==== | ==== main.cpp ==== | ||
<code>< | <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"); | |||
CSplashScreen''' aSplashScreen = new CSplashScreen(aSplashImage); | |||
aSplashScreen->show(); | |||
for (int i = 1; i | 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; | 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; pixmap);
void clearMessage();
void showMessage(const QString&amp; theMessage, int theAlignment = Qt::AlignLeft, const QColor&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; 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; theMessage, int theAlignment/* = Qt::AlignLeft*/, const QColor&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;
}