Remove usages of deprecated APIs of QDesktopWidget

- Replaced the usages of the following deprecated APIs:
  * QDesktopWidget::screenCount() -> QGuiApplication::screens().size()
  * QDesktopWidget::screenGeometry(int) -> QGuiApplication::screens().at()
  * QDesktopWidget::screenNumber(QPoint) -> QGuiApplication::screenAt(QPoint)

- Added notes for the QWidget *QDesktopWidget::screen(int), which
currently has no replacement.

- Fixed the tests to build conditionally, only when these APIs are
enabled.

Task-number: QTBUG-76491
Change-Id: I2fdec96d0a6a4fc782c53549b05a5556412b8305
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Sona Kurazyan 2019-07-23 14:33:31 +02:00
parent a90899df43
commit 66a9c4b0b2
6 changed files with 28 additions and 13 deletions

View File

@ -211,7 +211,9 @@ QDesktopWidget::QDesktopWidget()
setObjectName(QLatin1String("desktop")); setObjectName(QLatin1String("desktop"));
d->_q_updateScreens(); d->_q_updateScreens();
connect(qApp, SIGNAL(screenAdded(QScreen*)), this, SLOT(_q_updateScreens())); connect(qApp, SIGNAL(screenAdded(QScreen*)), this, SLOT(_q_updateScreens()));
#if QT_DEPRECATED_SINCE(5, 11)
connect(qApp, SIGNAL(primaryScreenChanged(QScreen*)), this, SIGNAL(primaryScreenChanged())); connect(qApp, SIGNAL(primaryScreenChanged(QScreen*)), this, SIGNAL(primaryScreenChanged()));
#endif
} }
QDesktopWidget::~QDesktopWidget() QDesktopWidget::~QDesktopWidget()

View File

@ -99,7 +99,7 @@ static QAlphaWidget* q_blend = 0;
Constructs a QAlphaWidget. Constructs a QAlphaWidget.
*/ */
QT_WARNING_PUSH QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED // QDesktopWidget::screen() QT_WARNING_DISABLE_DEPRECATED // ### Qt 6: Find a replacement for QDesktopWidget::screen()
QAlphaWidget::QAlphaWidget(QWidget* w, Qt::WindowFlags f) QAlphaWidget::QAlphaWidget(QWidget* w, Qt::WindowFlags f)
: QWidget(QApplication::desktop()->screen(QDesktopWidgetPrivate::screenNumber(w)), f) : QWidget(QApplication::desktop()->screen(QDesktopWidgetPrivate::screenNumber(w)), f)
{ {

View File

@ -42,10 +42,12 @@ class tst_QDesktopWidget : public QObject
private slots: private slots:
void cleanup(); void cleanup();
#if QT_DEPRECATED_SINCE(5, 11)
void numScreens(); void numScreens();
void primaryScreen(); void primaryScreen();
void screenNumberForQWidget();
void screenNumberForQPoint(); void screenNumberForQPoint();
#endif
void screenNumberForQWidget();
void availableGeometry(); void availableGeometry();
void screenGeometry(); void screenGeometry();
void topLevels(); void topLevels();
@ -56,6 +58,7 @@ void tst_QDesktopWidget::cleanup()
QVERIFY(QApplication::topLevelWidgets().isEmpty()); QVERIFY(QApplication::topLevelWidgets().isEmpty());
} }
#if QT_DEPRECATED_SINCE(5, 11)
void tst_QDesktopWidget::numScreens() void tst_QDesktopWidget::numScreens()
{ {
QDesktopWidget desktop; QDesktopWidget desktop;
@ -68,14 +71,17 @@ void tst_QDesktopWidget::primaryScreen()
QVERIFY(desktop.primaryScreen() >= 0); QVERIFY(desktop.primaryScreen() >= 0);
QVERIFY(desktop.primaryScreen() < desktop.numScreens()); QVERIFY(desktop.primaryScreen() < desktop.numScreens());
} }
#endif
void tst_QDesktopWidget::availableGeometry() void tst_QDesktopWidget::availableGeometry()
{ {
QDesktopWidget desktop; QDesktopWidget desktop;
QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::availableGeometry(): Attempt " QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::availableGeometry(): Attempt "
"to get the available geometry of a null widget"); "to get the available geometry of a null widget");
desktop.availableGeometry((QWidget *)0); QRect r = desktop.availableGeometry(nullptr);
QVERIFY(r.isNull());
#if QT_DEPRECATED_SINCE(5, 11)
QRect total; QRect total;
QRect available; QRect available;
@ -90,13 +96,14 @@ void tst_QDesktopWidget::availableGeometry()
QVERIFY(total.contains(available)); QVERIFY(total.contains(available));
QCOMPARE(desktop.availableGeometry(desktop.primaryScreen()), available); QCOMPARE(desktop.availableGeometry(desktop.primaryScreen()), available);
QCOMPARE(desktop.screenGeometry(desktop.primaryScreen()), total); QCOMPARE(desktop.screenGeometry(desktop.primaryScreen()), total);
#endif
} }
void tst_QDesktopWidget::screenNumberForQWidget() void tst_QDesktopWidget::screenNumberForQWidget()
{ {
QDesktopWidget desktop; QDesktopWidget desktop;
QCOMPARE(desktop.screenNumber(0), 0); QCOMPARE(desktop.screenNumber(nullptr), 0);
QWidget widget; QWidget widget;
widget.show(); widget.show();
@ -105,9 +112,10 @@ void tst_QDesktopWidget::screenNumberForQWidget()
int widgetScreen = desktop.screenNumber(&widget); int widgetScreen = desktop.screenNumber(&widget);
QVERIFY(widgetScreen > -1); QVERIFY(widgetScreen > -1);
QVERIFY(widgetScreen < desktop.numScreens()); QVERIFY(widgetScreen < QGuiApplication::screens().size());
} }
#if QT_DEPRECATED_SINCE(5, 11)
void tst_QDesktopWidget::screenNumberForQPoint() void tst_QDesktopWidget::screenNumberForQPoint()
{ {
// make sure QDesktopWidget::screenNumber(QPoint) returns the correct screen // make sure QDesktopWidget::screenNumber(QPoint) returns the correct screen
@ -131,25 +139,28 @@ void tst_QDesktopWidget::screenNumberForQPoint()
screen = desktopWidget->screenNumber(allScreens.bottomRight() + QPoint(1, 1)); screen = desktopWidget->screenNumber(allScreens.bottomRight() + QPoint(1, 1));
QVERIFY(screen >= 0 && screen < desktopWidget->numScreens()); QVERIFY(screen >= 0 && screen < desktopWidget->numScreens());
} }
#endif
void tst_QDesktopWidget::screenGeometry() void tst_QDesktopWidget::screenGeometry()
{ {
QDesktopWidget *desktopWidget = QApplication::desktop(); QDesktopWidget *desktopWidget = QApplication::desktop();
QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::screenGeometry(): Attempt " QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::screenGeometry(): Attempt "
"to get the screen geometry of a null widget"); "to get the screen geometry of a null widget");
QRect r = desktopWidget->screenGeometry((QWidget *)0); QRect r = desktopWidget->screenGeometry(nullptr);
QVERIFY(r.isNull()); QVERIFY(r.isNull());
QWidget widget; QWidget widget;
widget.show(); widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget)); QVERIFY(QTest::qWaitForWindowExposed(&widget));
r = desktopWidget->screenGeometry(&widget); r = desktopWidget->screenGeometry(&widget);
#if QT_DEPRECATED_SINCE(5, 11)
QRect total; QRect total;
QRect available; QRect available;
for (int i = 0; i < desktopWidget->screenCount(); ++i) { for (int i = 0; i < desktopWidget->screenCount(); ++i) {
total = desktopWidget->screenGeometry(i); total = desktopWidget->screenGeometry(i);
available = desktopWidget->availableGeometry(i); available = desktopWidget->availableGeometry(i);
} }
#endif
} }
void tst_QDesktopWidget::topLevels() void tst_QDesktopWidget::topLevels()

View File

@ -356,15 +356,12 @@ int main(int argc, char *argv[])
QApplication app(argc, argv); QApplication app(argc, argv);
MainWindowPtrList windows; MainWindowPtrList windows;
QDesktopWidget *desktopWidget = app.desktop();
const int lastScreen = arguments.contains("-p") const int lastScreen = arguments.contains("-p")
? 0 // Primary screen only ? 0 // Primary screen only
: desktopWidget->screenCount() - 1; // All screens : QGuiApplication::screens().size() - 1; // All screens
for (int s = lastScreen; s >= 0; --s) { for (int s = lastScreen; s >= 0; --s) {
MainWindowPtr window(new MainWindow(desktopWidget->screen(s))); MainWindowPtr window(new MainWindow());
const QPoint pos = desktopWidget->screenGeometry(s).center() - QPoint(200, 100); const QPoint pos = QGuiApplication::screens().at(s)->geometry().center() - QPoint(200, 100);
window->move(pos); window->move(pos);
windows.append(window); windows.append(window);
window->show(); window->show();

View File

@ -34,6 +34,7 @@
class DesktopView : public QGraphicsView class DesktopView : public QGraphicsView
{ {
#if QT_DEPRECATED_SINCE(5, 11)
Q_OBJECT Q_OBJECT
public: public:
DesktopView() DesktopView()
@ -195,6 +196,7 @@ private:
QGraphicsScene *scene; QGraphicsScene *scene;
QGraphicsRectItem *that; QGraphicsRectItem *that;
QPoint thatRoot; QPoint thatRoot;
#endif
}; };
#include "main.moc" #include "main.moc"

View File

@ -61,8 +61,10 @@ public:
QLatin1String("Left-click to test QGuiApplication::topLevelAt(click pos)\nRight-click to ungrab\n") : QLatin1String("Left-click to test QGuiApplication::topLevelAt(click pos)\nRight-click to ungrab\n") :
QLatin1String("Left-click to grab mouse\n"); QLatin1String("Left-click to grab mouse\n");
if (!m_cursorPos.isNull()) { if (!m_cursorPos.isNull()) {
const auto screen = QGuiApplication::screenAt(m_cursorPos);
const auto screenNum = screen ? QGuiApplication::screens().indexOf(screen) : 0;
txt += QString(QLatin1String("Current mouse position: %1, %2 on screen %3\n")) txt += QString(QLatin1String("Current mouse position: %1, %2 on screen %3\n"))
.arg(m_cursorPos.x()).arg(m_cursorPos.y()).arg(QApplication::desktop()->screenNumber(m_cursorPos)); .arg(m_cursorPos.x()).arg(m_cursorPos.y()).arg(screenNum);
if (QGuiApplication::mouseButtons() & Qt::LeftButton) { if (QGuiApplication::mouseButtons() & Qt::LeftButton) {
QWindow *win = QGuiApplication::topLevelAt(m_cursorPos); QWindow *win = QGuiApplication::topLevelAt(m_cursorPos);
txt += QString(QLatin1String("Top-level window found? %1\n")) txt += QString(QLatin1String("Top-level window found? %1\n"))
@ -234,6 +236,7 @@ void screenAdded(QScreen* screen)
QList<QScreen *> screens = QGuiApplication::screens(); QList<QScreen *> screens = QGuiApplication::screens();
int screenNumber = screens.indexOf(screen); int screenNumber = screens.indexOf(screen);
Q_ASSERT(screenNumber >= 0); Q_ASSERT(screenNumber >= 0);
// ### Qt 6: Find a replacement for QDesktopWidget::screen()
w->setParent(qApp->desktop()->screen(screenNumber)); w->setParent(qApp->desktop()->screen(screenNumber));
w->show(); w->show();