Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 void handlePaintEvent();
37};
38
100 : QWidget(*(new QSplashScreenPrivate()), nullptr, Qt::SplashScreen | Qt::FramelessWindowHint | f)
101{
102 setPixmap(pixmap); // Does an implicit repaint
103}
104
115 : QWidget(*(new QSplashScreenPrivate()), nullptr, Qt::SplashScreen | Qt::FramelessWindowHint | f)
116{
117 Q_D(QSplashScreen);
118 d->setScreen(screen);
120}
121
128
136
147
170 const QColor &color)
171{
172 Q_D(QSplashScreen);
173 d->currStatus = message;
174 d->currAlign = alignment;
175 d->currColor = color;
176 emit messageChanged(d->currStatus);
177 repaint();
178}
179
189{
190 Q_D(const QSplashScreen);
191 return d->currStatus;
192}
193
200{
201 d_func()->currStatus.clear();
202 emit messageChanged(d_func()->currStatus);
203 repaint();
204}
205
206static bool waitForWidgetMapped(QWidget *widget, int timeout = 1000)
207{
208 enum { TimeOutMs = 10 };
209 auto isMapped = [widget](){
210 return widget->windowHandle() && widget->windowHandle()->isVisible();
211 };
212
214 timer.start();
215 while (!isMapped()) {
216 const int remaining = timeout - int(timer.elapsed());
217 if (remaining <= 0)
218 break;
221#if defined(Q_OS_WIN)
222 Sleep(uint(TimeOutMs));
223#else
224 struct timespec ts = { TimeOutMs / 1000, (TimeOutMs % 1000) * 1000 * 1000 };
225 nanosleep(&ts, nullptr);
226#endif
227 }
228 return isMapped();
229}
230
237{
238 if (mainWin) {
239 if (!mainWin->windowHandle())
240 mainWin->createWinId();
241 waitForWidgetMapped(mainWin);
242 }
243 close();
244}
245
251{
252 Q_D(QSplashScreen);
253 d->pixmap = pixmap;
255
257 resize(r.size());
258
259 move(screen()->geometry().center() - r.center());
260 if (isVisible())
261 repaint();
262}
263
269{
270 return d_func()->pixmap;
271}
272
276inline QSplashScreenPrivate::QSplashScreenPrivate() : currAlign(Qt::AlignLeft)
277{
278}
279
287{
288 Q_D(QSplashScreen);
289 painter->setPen(d->currColor);
290 QRect r = rect().adjusted(5, 5, -5, -5);
291 if (Qt::mightBeRichText(d->currStatus)) {
292 QTextDocument doc;
293#ifdef QT_NO_TEXTHTMLPARSER
294 doc.setPlainText(d->currStatus);
295#else
296 doc.setHtml(d->currStatus);
297#endif
298 doc.setTextWidth(r.width());
299 QTextCursor cursor(&doc);
302 fmt.setAlignment(Qt::Alignment(d->currAlign));
303 fmt.setLayoutDirection(layoutDirection());
304 cursor.mergeBlockFormat(fmt);
305 const QSizeF txtSize = doc.size();
306 if (d->currAlign & Qt::AlignBottom)
307 r.setTop(r.height() - txtSize.height());
308 else if (d->currAlign & Qt::AlignVCenter)
309 r.setTop(r.height() / 2 - txtSize.height() / 2);
310 painter->save();
311 painter->translate(r.topLeft());
313 painter->restore();
314 } else {
315 painter->drawText(r, d->currAlign, d->currStatus);
316 }
317}
318
329
332{
333 Q_D(QSplashScreen);
334
335 switch (e->type()) {
336 case QEvent::Paint:
337 d->handlePaintEvent();
338 break;
339 case QEvent::Show:
341 break;
342 default:
343 break;
344 }
345
346 return QWidget::event(e);
347}
348
350
351#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
Type type() const
Returns the event type.
Definition qcoreevent.h:304
\inmodule QtGui
Definition qevent.h:196
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:626
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition qpixmap.cpp:456
bool hasAlpha() const
\inmodule QtCore\reentrant
Definition qpoint.h:25
\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:370
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
\inmodule QtCore
Definition qsize.h:208
constexpr QSize toSize() const noexcept
Returns an integer based copy of this size.
Definition qsize.h:401
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:129
void setAlignment(Qt::Alignment alignment)
Sets the paragraph's alignment.
\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:241
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:8562
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 hide()
Hides the widget.
Definition qwidget.cpp:8135
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:2483
bool event(QEvent *event) override
This is the main event handler; it handles event event.
Definition qwidget.cpp:8866
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:2496
bool isVisible() const
Definition qwidget.h:874
QOpenGLWidget * widget
[1]
uint alignment
Combined button and popup list for selecting options.
Definition qcompare.h:63
@ AlignBottom
Definition qnamespace.h:154
@ AlignVCenter
Definition qnamespace.h:155
@ WA_TranslucentBackground
Definition qnamespace.h:402
Q_GUI_EXPORT bool mightBeRichText(QAnyStringView)
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 color
[2]
GLuint GLsizei const GLchar * message
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
static bool waitForWidgetMapped(QWidget *widget, int timeout=1000)
QScreen * screen
[1]
Definition main.cpp:29
#define emit
unsigned int uint
Definition qtypes.h:34
QVideoFrameFormat::PixelFormat fmt
QObject::connect nullptr
QTimer * timer
[3]
widget render & pixmap
QPainter painter(this)
[7]