Add a QSplashScreen constructor taking a QScreen
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ø <tor.arne.vestbo@qt.io>bb10
parent
e77877f207
commit
49362d064f
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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<QDesktopWidgetPrivate *>(qt_widget_private(QApplication::desktop()));
|
||||
return desktopWidgetPrivate->screens.indexOf(const_cast<QDesktopScreenWidget *>(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<const QDesktopScreenWidget *>(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.
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue