Add screen() accessor to QWidget

Base it on QWidgetPrivate::associatedScreen(), but make a larger
effort to find a screen in case the widget is not shown yet.

Rename QDesktopScreenWidget::screen() to something else to avoid
clashes.

Task-number: QTBUG-62094
Task-number: QTBUG-53022
Change-Id: I36ba5ef5f0645a4ac89da0b38a391f7057b2f49c
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Friedemann Kleint 2019-04-05 14:20:06 +02:00
parent e71bf9d5c7
commit b455a863a1
5 changed files with 29 additions and 3 deletions

View File

@ -111,7 +111,7 @@ const QRect QDesktopWidgetPrivate::availableGeometry(const QWidget *widget)
QDesktopScreenWidget *QDesktopWidgetPrivate::widgetForScreen(QScreen *qScreen) const
{
foreach (QDesktopScreenWidget *widget, screens) {
if (widget->screen() == qScreen)
if (widget->assignedScreen() == qScreen)
return widget;
}
return nullptr;

View File

@ -68,7 +68,7 @@ public:
int screenNumber() const;
void setScreenGeometry(const QRect &geometry);
QScreen *screen() const { return m_screen.data(); }
QScreen *assignedScreen() const { return m_screen.data(); }
QRect screenGeometry() const { return m_geometry; }
private:

View File

@ -2445,7 +2445,7 @@ WId QWidget::effectiveWinId() const
\since 5.0
\sa winId()
\sa winId(), screen()
*/
QWindow *QWidget::windowHandle() const
{
@ -2453,6 +2453,29 @@ QWindow *QWidget::windowHandle() const
return d->windowHandle();
}
/*!
Returns the screen the widget is on.
\since 5.14
\sa windowHandle()
*/
QScreen *QWidget::screen() const
{
Q_D(const QWidget);
if (auto associatedScreen = d->associatedScreen())
return associatedScreen;
if (auto topLevel = window()) {
if (auto topData = qt_widget_private(topLevel)->topData()) {
if (auto initialScreen = QGuiApplicationPrivate::screen_list.value(topData->initialScreenIndex))
return initialScreen;
}
if (auto screenByPos = QGuiApplication::screenAt(topLevel->geometry().center()))
return screenByPos;
}
return QGuiApplication::primaryScreen();
}
#ifndef QT_NO_STYLE_STYLESHEET
/*!

View File

@ -85,6 +85,7 @@ class QDragEnterEvent;
class QDragMoveEvent;
class QDragLeaveEvent;
class QDropEvent;
class QScreen;
class QShowEvent;
class QHideEvent;
class QIcon;
@ -601,6 +602,7 @@ public:
QBackingStore *backingStore() const;
QWindow *windowHandle() const;
QScreen *screen() const;
static QWidget *createWindowContainer(QWindow *window, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags());

View File

@ -1139,6 +1139,7 @@ void tst_QWidget::visible()
QVERIFY( !childWidget->isVisible() );
testWidget->show();
QVERIFY(testWidget->screen());
QVERIFY( testWidget->isVisible() );
QVERIFY( childWidget->isVisible() );