Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsplashscreen.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsplashscreen.h"
5
6#include "qapplication.h"
7#include "qpainter.h"
8#include "qpixmap.h"
9#include "qtextdocument.h"
10#include "qtextcursor.h"
11#include <QtGui/qscreen.h>
12#include <QtGui/qwindow.h>
13#include <QtCore/qdebug.h>
14#include <QtCore/qelapsedtimer.h>
15#include <private/qwidget_p.h>
16
17#ifdef Q_OS_WIN
18# include <QtCore/qt_windows.h>
19#else
20# include <time.h>
21#endif
22
24
26{
27 Q_DECLARE_PUBLIC(QSplashScreen)
28public:
33
34 inline QSplashScreenPrivate();
35};
36
98 : QWidget(*(new QSplashScreenPrivate()), nullptr, Qt::SplashScreen | Qt::FramelessWindowHint | f)
99{
100 setPixmap(pixmap); // Does an implicit repaint
101}
102
113 : QWidget(*(new QSplashScreenPrivate()), nullptr, Qt::SplashScreen | Qt::FramelessWindowHint | f)
114{
115 Q_D(QSplashScreen);
116 d->setScreen(screen);
118}
119
124{
125}
126
131{
132 hide();
133}
134
141{
144}
145
168 const QColor &color)
169{
170 Q_D(QSplashScreen);
171 d->currStatus = message;
172 d->currAlign = alignment;
173 d->currColor = color;
174 emit messageChanged(d->currStatus);
175 repaint();
176}
177
187{
188 Q_D(const QSplashScreen);
189 return d->currStatus;
190}
191
198{
199 d_func()->currStatus.clear();
200 emit messageChanged(d_func()->currStatus);
201 repaint();
202}
203
204// A copy of Qt Test's qWaitForWindowExposed() and qSleep().
205inline static bool waitForWindowExposed(QWindow *window, int timeout = 1000)
206{
207 enum { TimeOutMs = 10 };
209 timer.start();
210 while (!window->isExposed()) {
211 const int remaining = timeout - int(timer.elapsed());
212 if (remaining <= 0)
213 break;
216#if defined(Q_OS_WIN)
217 Sleep(uint(TimeOutMs));
218#else
219 struct timespec ts = { TimeOutMs / 1000, (TimeOutMs % 1000) * 1000 * 1000 };
220 nanosleep(&ts, nullptr);
221#endif
222 }
223 return window->isExposed();
224}
225
232{
233 if (mainWin) {
234 if (!mainWin->windowHandle())
235 mainWin->createWinId();
237 }
238 close();
239}
240
246{
247 Q_D(QSplashScreen);
248 d->pixmap = pixmap;
250
252 resize(r.size());
253
254 move(screen()->geometry().center() - r.center());
255 if (isVisible())
256 repaint();
257}
258
264{
265 return d_func()->pixmap;
266}
267
271inline QSplashScreenPrivate::QSplashScreenPrivate() : currAlign(Qt::AlignLeft)
272{
273}
274
282{
283 Q_D(QSplashScreen);
284 painter->setPen(d->currColor);
285 QRect r = rect().adjusted(5, 5, -5, -5);
286 if (Qt::mightBeRichText(d->currStatus)) {
287 QTextDocument doc;
288#ifdef QT_NO_TEXTHTMLPARSER
289 doc.setPlainText(d->currStatus);
290#else
291 doc.setHtml(d->currStatus);
292#endif
293 doc.setTextWidth(r.width());
294 QTextCursor cursor(&doc);
297 fmt.setAlignment(Qt::Alignment(d->currAlign));
298 fmt.setLayoutDirection(layoutDirection());
299 cursor.mergeBlockFormat(fmt);
300 const QSizeF txtSize = doc.size();
301 if (d->currAlign & Qt::AlignBottom)
302 r.setTop(r.height() - txtSize.height());
303 else if (d->currAlign & Qt::AlignVCenter)
304 r.setTop(r.height() / 2 - txtSize.height() / 2);
305 painter->save();
306 painter->translate(r.topLeft());
308 painter->restore();
309 } else {
310 painter->drawText(r, d->currAlign, d->currStatus);
311 }
312}
313
316{
317 if (e->type() == QEvent::Paint) {
318 Q_D(QSplashScreen);
319 QPainter painter(this);
322 if (!d->pixmap.isNull())
323 painter.drawPixmap(QPoint(), d->pixmap);
325 }
326 return QWidget::event(e);
327}
328
330
331#include "moc_qsplashscreen.cpp"
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
Processes some pending events for the calling thread according to the specified flags.
static void sendPostedEvents(QObject *receiver=nullptr, int event_type=0)
Immediately dispatches all events which have been previously queued with QCoreApplication::postEvent(...
\inmodule QtCore
\inmodule QtCore
Definition qcoreevent.h:45
@ DeferredDelete
Definition qcoreevent.h:100
\inmodule QtGui
Definition qevent.h:195
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void setPen(const QColor &color)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setLayoutDirection(Qt::LayoutDirection direction)
Sets the layout direction used by the painter when drawing text, to the specified direction.
void restore()
Restores the current painter state (pops a saved state off the stack).
void save()
Saves the current painter state (pushes the state onto a stack).
void drawText(const QPointF &p, const QString &s)
Draws the given text with the currently defined text direction, beginning at the given position.
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device.
@ SmoothPixmapTransform
Definition qpainter.h:54
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
void setRenderHints(RenderHints hints, bool on=true)
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
QSizeF deviceIndependentSize() const
Returns the size of the pixmap in device independent pixels.
Definition qpixmap.cpp:630
bool hasAlpha() const
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr QRect adjusted(int x1, int y1, int x2, int y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:369
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
\inmodule QtCore
Definition qsize.h:207
constexpr QSize toSize() const noexcept
Returns an integer based copy of this size.
Definition qsize.h:390
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
The QSplashScreen widget provides a splash screen that can be shown during application startup.
QSplashScreen(const QPixmap &pixmap=QPixmap(), Qt::WindowFlags f=Qt::WindowFlags())
Construct a splash screen that will display the pixmap.
void messageChanged(const QString &message)
This signal is emitted when the message on the splash screen changes.
void setPixmap(const QPixmap &pixmap)
Sets the pixmap that will be used as the splash screen's image to pixmap.
void repaint()
This overrides QWidget::repaint().
const QPixmap pixmap() const
Returns the pixmap that is used in the splash screen.
bool event(QEvent *e) override
\reimp
void clearMessage()
Removes the message being displayed on the splash screen.
void finish(QWidget *w)
Makes the splash screen wait until the widget mainWin is displayed before calling close() on itself.
void showMessage(const QString &message, int alignment=Qt::AlignLeft, const QColor &color=Qt::black)
Draws the message text onto the splash screen with color color and aligns the text according to the f...
void mousePressEvent(QMouseEvent *) override
\reimp
virtual ~QSplashScreen()
Destructor.
QString message() const
virtual void drawContents(QPainter *painter)
Draw the contents of the splash screen using painter painter.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
\reentrant \inmodule QtGui
void setHtml(const QString &html)
Replaces the entire contents of the document with the given HTML-formatted text in the html string.
QSizeF size
the actual size of the document. This is equivalent to documentLayout()->documentSize();
void setPlainText(const QString &text)
Replaces the entire contents of the document with the given plain text.
void setTextWidth(qreal width)
void drawContents(QPainter *painter, const QRectF &rect=QRectF())
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition qtimer.cpp:208
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
void repaint()
Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the ...
Qt::LayoutDirection layoutDirection
the layout direction for this widget.
Definition qwidget.h:170
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition qwidget.h:106
bool close()
Closes this widget.
Definition qwidget.cpp:8608
void move(int x, int y)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:880
void createWinId()
Definition qwidget.cpp:2448
void hide()
Hides the widget.
Definition qwidget.cpp:8209
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
QWindow * windowHandle() const
If this is a native widget, return the associated QWindow.
Definition qwidget.cpp:2490
bool event(QEvent *event) override
This is the main event handler; it handles event event.
Definition qwidget.cpp:8912
void resize(int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:883
QCursor cursor
the cursor shape for this widget
Definition qwidget.h:135
QScreen * screen() const
Returns the screen the widget is on.
Definition qwidget.cpp:2503
bool isVisible() const
Definition qwidget.h:874
\inmodule QtGui
Definition qwindow.h:63
double e
uint alignment
Combined button and popup list for selecting options.
@ AlignBottom
Definition qnamespace.h:153
@ AlignVCenter
Definition qnamespace.h:154
@ WA_TranslucentBackground
Definition qnamespace.h:401
Q_GUI_EXPORT bool mightBeRichText(const QString &)
Returns true if the string text is likely to be rich text; otherwise returns false.
GLboolean r
[2]
GLbitfield GLuint64 timeout
[4]
GLfloat GLfloat f
GLuint GLsizei const GLchar * message
static bool waitForWindowExposed(QWindow *window, int timeout=1000)
QScreen * screen
[1]
Definition main.cpp:29
#define emit
unsigned int uint
Definition qtypes.h:29
QVideoFrameFormat::PixelFormat fmt
QObject::connect nullptr
QTimer * timer
[3]
widget render & pixmap
QPainter painter(this)
[7]
aWidget window() -> setWindowTitle("New Window Title")
[2]