From 49362d064fffe350600f5324fb510b381578d04a Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Wed, 4 Sep 2019 17:26:33 +0200 Subject: Add a QSplashScreen constructor taking a QScreen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The constructor taking a QWidget is needed for specifying the screen where the splash screen should be displayed. Add a new constructor for specifying the target screen for the splash screen directly, instead of "extracting" the screen information from a widget. This removes the need for using the deprecated QDesktopWidget. Deprecate the constructor taking a QWidget. Task-number: QTBUG-76491 Change-Id: I1dde242ff5f7b53e52af308bb685f492d6266d33 Reviewed-by: Tor Arne Vestbø --- src/widgets/doc/snippets/qsplashscreen/main.cpp | 7 ++++ src/widgets/widgets/qsplashscreen.cpp | 38 ++++++++++++++++++++-- src/widgets/widgets/qsplashscreen.h | 4 +++ .../widgets/qsplashscreen/tst_qsplashscreen.cpp | 12 +++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/widgets/doc/snippets/qsplashscreen/main.cpp b/src/widgets/doc/snippets/qsplashscreen/main.cpp index 843932ca83..2512035879 100644 --- a/src/widgets/doc/snippets/qsplashscreen/main.cpp +++ b/src/widgets/doc/snippets/qsplashscreen/main.cpp @@ -71,3 +71,10 @@ int main(int argc, char *argv[]) return app.exec(); } //! [1] + +//! [2] +QScreen *screen = QGuiApplication::screens().at(1); +QPixmap pixmap(":/splash.png"); +QSplashScreen splash(screen, pixmap); +splash.show(); +//! [2] diff --git a/src/widgets/widgets/qsplashscreen.cpp b/src/widgets/widgets/qsplashscreen.cpp index e39ef6d1cd..b7c3426e08 100644 --- a/src/widgets/widgets/qsplashscreen.cpp +++ b/src/widgets/widgets/qsplashscreen.cpp @@ -123,6 +123,11 @@ public: wish to do your own drawing you can get a pointer to the pixmap used in the splash screen with pixmap(). Alternatively, you can subclass QSplashScreen and reimplement drawContents(). + + In case of having multiple screens, it is also possible to show the + splash screen on a different screen than the primary one. For example: + + \snippet qsplashscreen/main.cpp 2 */ /*! @@ -139,6 +144,23 @@ QSplashScreen::QSplashScreen(const QPixmap &pixmap, Qt::WindowFlags f) /*! \overload + \since 5.15 + + This function allows you to specify the screen for your splashscreen. The + typical use for this constructor is if you have multiple screens and + prefer to have the splash screen on a different screen than your primary + one. In that case pass the proper \a screen. +*/ +QSplashScreen::QSplashScreen(QScreen *screen, const QPixmap &pixmap, Qt::WindowFlags f) + : QWidget(*(new QSplashScreenPrivate()), nullptr, Qt::SplashScreen | Qt::FramelessWindowHint | f) +{ + d_func()->setPixmap(pixmap, screen); +} + +#if QT_DEPRECATED_SINCE(5, 15) +/*! + \overload + \obsolete This function allows you to specify a parent for your splashscreen. The typical use for this constructor is if you have a multiple screens and @@ -152,6 +174,7 @@ QSplashScreen::QSplashScreen(QWidget *parent, const QPixmap &pixmap, Qt::WindowF // is still 0 here due to QWidget's special handling. d_func()->setPixmap(pixmap, QSplashScreenPrivate::screenFor(parent)); } +#endif /*! Destructor. @@ -286,26 +309,35 @@ void QSplashScreen::setPixmap(const QPixmap &pixmap) } // In setPixmap(), resize and try to position on a screen according to: -// 1) If a QDesktopScreenWidget is found in the parent hierarchy, use that (see docs on +// 1) If the screen for the given widget is available, use that +// 2) If a QDesktopScreenWidget is found in the parent hierarchy, use that (see docs on // QSplashScreen(QWidget *, QPixmap). -// 2) If a widget with associated QWindow is found, use that -// 3) When nothing can be found, try to center it over the cursor +// 3) If a widget with associated QWindow is found, use that +// 4) When nothing can be found, try to center it over the cursor +#if QT_DEPRECATED_SINCE(5, 15) static inline int screenNumberOf(const QDesktopScreenWidget *dsw) { auto desktopWidgetPrivate = static_cast(qt_widget_private(QApplication::desktop())); return desktopWidgetPrivate->screens.indexOf(const_cast(dsw)); } +#endif const QScreen *QSplashScreenPrivate::screenFor(const QWidget *w) { + if (w && w->screen()) + return w->screen(); + for (const QWidget *p = w; p !=nullptr ; p = p->parentWidget()) { +#if QT_DEPRECATED_SINCE(5, 15) if (auto dsw = qobject_cast(p)) return QGuiApplication::screens().value(screenNumberOf(dsw)); +#endif if (QWindow *window = p->windowHandle()) return window->screen(); } + #if QT_CONFIG(cursor) // Note: We could rely on QPlatformWindow::initialGeometry() to center it // over the cursor, but not all platforms (namely Android) use that. diff --git a/src/widgets/widgets/qsplashscreen.h b/src/widgets/widgets/qsplashscreen.h index 8bdf4e7749..1877493fcf 100644 --- a/src/widgets/widgets/qsplashscreen.h +++ b/src/widgets/widgets/qsplashscreen.h @@ -55,7 +55,11 @@ class Q_WIDGETS_EXPORT QSplashScreen : public QWidget Q_OBJECT public: explicit QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags()); + QSplashScreen(QScreen *screen, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags()); +#if QT_DEPRECATED_SINCE(5, 15) + QT_DEPRECATED_VERSION_X_5_15("Use the constructor taking a QScreen *") QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags()); +#endif virtual ~QSplashScreen(); void setPixmap(const QPixmap &pixmap); diff --git a/tests/auto/widgets/widgets/qsplashscreen/tst_qsplashscreen.cpp b/tests/auto/widgets/widgets/qsplashscreen/tst_qsplashscreen.cpp index 91a9c49b00..64e4582366 100644 --- a/tests/auto/widgets/widgets/qsplashscreen/tst_qsplashscreen.cpp +++ b/tests/auto/widgets/widgets/qsplashscreen/tst_qsplashscreen.cpp @@ -36,6 +36,7 @@ class tst_QSplashScreen : public QObject private slots: void checkCloseTime(); + void checkScreenConstructor(); }; class CloseEventSplash : public QSplashScreen @@ -69,5 +70,16 @@ void tst_QSplashScreen::checkCloseTime() QVERIFY(w.windowHandle()->isExposed()); } +void tst_QSplashScreen::checkScreenConstructor() +{ + for (const auto screen : QGuiApplication::screens()) { + QSplashScreen splash(screen); + splash.show(); + QCOMPARE(splash.screen(), screen); + QVERIFY(splash.windowHandle()); + QCOMPARE(splash.windowHandle()->screen(), screen); + } +} + QTEST_MAIN(tst_QSplashScreen) #include "tst_qsplashscreen.moc" -- cgit v1.2.3