Get rid of all usage of QApplication:desktop

Use QScreen APIs instead.

Change-Id: Ie99af94fe4292223dbb165b3f5c1b74e8fe0498b
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2020-10-13 21:00:21 +02:00
parent 772a10391e
commit 1c76aa077e
14 changed files with 28 additions and 63 deletions

View File

@ -50,6 +50,7 @@
#include <QApplication>
#include <QSurfaceFormat>
#include <QScreen>
#include <QCommandLineParser>
#include <QCommandLineOption>
@ -94,8 +95,8 @@ int main(int argc, char *argv[])
mainWindow.setAttribute(Qt::WA_NoSystemBackground, false);
}
mainWindow.resize(mainWindow.sizeHint());
int desktopArea = QApplication::desktop()->width() *
QApplication::desktop()->height();
int desktopArea = QGuiApplication::primaryScreen()->size().width() *
QGuiApplication::primaryScreen()->size().height();
int widgetArea = mainWindow.width() * mainWindow.height();
if (((float)widgetArea / (float)desktopArea) < 0.75f)
mainWindow.show();

View File

@ -122,8 +122,8 @@ void Window::dockUndock()
if (parent()) {
setParent(nullptr);
setAttribute(Qt::WA_DeleteOnClose);
move(QApplication::desktop()->width() / 2 - width() / 2,
QApplication::desktop()->height() / 2 - height() / 2);
move(QGuiApplication::primaryScreen()->size().width() / 2 - width() / 2,
QGuiApplication::primaryScreen()->size().height() / 2 - height() / 2);
dockBtn->setText(tr("Dock"));
show();
} else {

View File

@ -220,8 +220,7 @@ void QApplicationPrivate::createEventDispatcher()
\li It provides localization of strings that are visible to the
user via translate().
\li It provides some magical objects like the desktop() and the
clipboard().
\li It provides some magical objects like the clipboard().
\li It knows about the application's windows. You can ask which
widget is at a certain position using widgetAt(), get a list of

View File

@ -285,7 +285,7 @@ void tst_languageChange::retranslatability()
// In case we use a Color dialog, we do not want to test for
// strings non existing in the dialog and which do not get
// translated.
const QSize desktopSize = QApplication::desktop()->size();
const QSize desktopSize = QGuiApplication::primaryScreen()->size();
if (dialogType == ColorDialog && (desktopSize.width() < 480 || desktopSize.height() < 350)) {
expected.remove("QColorDialog::&Basic colors");
expected.remove("QColorDialog::&Custom colors");

View File

@ -409,7 +409,7 @@ void tst_QGraphicsEffect::opacity()
void tst_QGraphicsEffect::grayscale()
{
if (qApp->desktop()->depth() < 24)
if (QGuiApplication::primaryScreen()->depth() < 24)
QSKIP("Test only works on 32 bit displays");
QGraphicsScene scene(0, 0, 100, 100);
@ -456,7 +456,7 @@ void tst_QGraphicsEffect::grayscale()
void tst_QGraphicsEffect::colorize()
{
if (qApp->desktop()->depth() < 24)
if (QGuiApplication::primaryScreen()->depth() < 24)
QSKIP("Test only works on 32 bit displays");
QGraphicsScene scene(0, 0, 100, 100);

View File

@ -3433,14 +3433,14 @@ void tst_QHeaderView::statusTips()
QtTestModel model(5, 5);
headerView.setModel(&model);
headerView.viewport()->setMouseTracking(true);
headerView.setGeometry(QRect(QPoint(QApplication::desktop()->geometry().center() - QPoint(250, 250)),
headerView.setGeometry(QRect(QPoint(QGuiApplication::primaryScreen()->geometry().center() - QPoint(250, 250)),
QSize(500, 500)));
headerView.show();
QApplication::setActiveWindow(&headerView);
QVERIFY(QTest::qWaitForWindowActive(&headerView));
// Ensure it is moved away first and then moved to the relevant section
QTest::mouseMove(QApplication::desktop(),
QTest::mouseMove(&headerView,
headerView.rect().bottomLeft() + QPoint(20, 20));
QPoint centerPoint = QRect(headerView.sectionPosition(0), 0,
headerView.sectionSize(0), headerView.height()).center();

View File

@ -4787,7 +4787,7 @@ void tst_QTreeView::statusTip()
mw.setCentralWidget(view);
}
mw.statusBar();
mw.setGeometry(QRect(QPoint(QApplication::desktop()->geometry().center() - QPoint(250, 250)),
mw.setGeometry(QRect(QPoint(QGuiApplication::primaryScreen()->geometry().center() - QPoint(250, 250)),
QSize(500, 500)));
mw.show();
QApplication::setActiveWindow(&mw);

View File

@ -2415,7 +2415,6 @@ void tst_QApplication::staticFunctions()
QApplication::setStyle(QStringLiteral("blub"));
QApplication::allWidgets();
QApplication::topLevelWidgets();
QApplication::desktop();
QApplication::activePopupWidget();
QApplication::activeModalWidget();
QApplication::focusWidget();

View File

@ -6513,7 +6513,7 @@ void tst_QWidget::minAndMaxSizeWithX11BypassWindowManagerHint()
if (m_platform != QStringLiteral("xcb"))
QSKIP("This test is for X11 only.");
// Same size as in QWidgetPrivate::create.
const QSize desktopSize = QApplication::desktop()->size();
const QSize desktopSize = QGuiApplication::primaryScreen()->size();
const QSize originalSize(desktopSize.width() / 2, desktopSize.height() * 4 / 10);
{ // Maximum size.
@ -9262,13 +9262,11 @@ void tst_QWidget::translucentWidget()
label.show();
QVERIFY(QTest::qWaitForWindowExposed(&label));
QPixmap widgetSnapshot;
QPixmap widgetSnapshot =
#ifdef Q_OS_WIN
QWidget *desktopWidget = QApplication::desktop();
widgetSnapshot = grabWindow(desktopWidget->windowHandle(), labelPos.x(), labelPos.y(), label.width(), label.height());
QGuiApplication::primaryScreen()->grabWindow(0, labelPos.x(), labelPos.y(), label.width(), label.height());
#else
widgetSnapshot = label.grab(QRect(QPoint(0, 0), label.size()));
label.grab(QRect(QPoint(0, 0), label.size()));
#endif
const QImage actual = widgetSnapshot.toImage().convertToFormat(QImage::Format_RGB32);
QImage expected = pm.toImage().scaled(label.devicePixelRatio() * pm.size());

View File

@ -901,7 +901,7 @@ void tst_QMdiArea::sizeHint()
{
QMdiArea workspace;
workspace.show();
QSize desktopSize = QApplication::desktop()->size();
QSize desktopSize = QGuiApplication::primaryScreen()->size();
QSize expectedSize(desktopSize.width() * 2/3, desktopSize.height() * 2/3);
QCOMPARE(workspace.sizeHint(), expectedSize);

View File

@ -504,23 +504,19 @@ static inline QString msgRgbMismatch(unsigned actual, unsigned expected)
static QPixmap grabWidgetWithoutRepaint(const QWidget *widget, QRect clipArea)
{
const QWidget *targetWidget = widget;
const QWindow *window = widget->window()->windowHandle();
Q_ASSERT(window);
WId windowId = window->winId();
#ifdef Q_OS_WIN
// OpenGL content is not properly grabbed on Windows when passing a top level widget window,
// because GDI functions can't grab OpenGL layer content.
// Instead the whole screen should be captured, with an adjusted clip area, which contains
// the final composited content.
QWidget *desktopWidget = QApplication::desktop(QGuiApplication::primaryScreen());
const QWidget *mainScreenWidget = desktopWidget;
targetWidget = mainScreenWidget;
windowId = 0;
clipArea = QRect(widget->mapToGlobal(clipArea.topLeft()),
widget->mapToGlobal(clipArea.bottomRight()));
#endif
const QWindow *window = targetWidget->window()->windowHandle();
Q_ASSERT(window);
WId windowId = window->winId();
QScreen *screen = window->screen();
Q_ASSERT(screen);

View File

@ -68,11 +68,11 @@ public:
void tryResize(int width, int height)
{
QWidget *desktop = QApplication::desktop();
if (desktop->width() < width)
width = desktop->width();
if (desktop->height() < height)
height = desktop->height();
const QSize desktopSize = QGuiApplication::primaryScreen()->size();
if (desktopSize.width() < width)
width = desktopSize.width();
if (desktopSize.height() < height)
height = desktopSize.height();
if (size() != QSize(width, height)) {
resize(width, height);
QTest::qWait(250);

View File

@ -228,12 +228,7 @@ void screenAdded(QScreen* screen)
(screen->virtualSiblings().isEmpty() ? "none" : qPrintable(screen->virtualSiblings().first()->name())));
ScreenWatcherMainWindow *w = new ScreenWatcherMainWindow(screen);
// Set the screen; this corresponds to setScreen() for the underlying
// QWindow. This is essential when having separate X screens since the
// positioning below is not sufficient to get the windows show up on the
// desired screen.
w->setParent(qApp->desktop(screen));
w->setScreen(screen);
w->show();
// Position the windows so that they end up at the center of the corresponding screen.

View File

@ -68,18 +68,12 @@ public:
screenButton->setEnabled(false);
connect(screenButton, &QAbstractButton::clicked, this, &Controller::setScreen);
QPushButton *desktopButton = new QPushButton;
desktopButton->setText("Show on Desktop");
desktopButton->setEnabled(false);
connect(desktopButton, &QAbstractButton::clicked, this, &Controller::setDesktop);
QPushButton *exitButton = new QPushButton;
exitButton->setText("E&xit");
connect(exitButton, &QAbstractButton::clicked, QApplication::instance(), &QCoreApplication::quit);
QHBoxLayout *actionLayout = new QHBoxLayout;
actionLayout->addWidget(screenButton);
actionLayout->addWidget(desktopButton);
actionLayout->addWidget(exitButton);
QGroupBox *radioGroup = new QGroupBox;
@ -95,7 +89,6 @@ public:
if (on)
targetScreen = count;
screenButton->setEnabled(targetScreen != -1);
desktopButton->setEnabled(targetScreen != -1);
});
groupLayout->addWidget(choice);
++count;
@ -122,24 +115,8 @@ private slots:
widget->updateText();
}
void setDesktop()
{
QScreen *screen = QGuiApplication::screens().at(targetScreen);
QWidget *desktop = QApplication::desktop(screen);
if (!desktopChild) {
desktopChild = new ScreenWidget(desktop);
desktopChild->setAttribute(Qt::WA_DeleteOnClose);
desktopChild->setWindowTitle("Child of a Desktop");
} else {
desktopChild->setParent(desktop);
}
desktopChild->show();
desktopChild->updateText();
}
private:
QPointer<ScreenWidget> widget = nullptr;
QPointer<ScreenWidget> desktopChild = nullptr;
int targetScreen = -1;
};