Skip to content
Permalink
Browse files
Use QApplication::activeWindow() instead of
QApplication::topLevelWidgets() to retrieve current screen

QApplication::topLevelWidgets() can  be EXTREMELY costly to call,
as it builds a list dynamically containing potentially dozens/hundreds
of widgets.

Fixes slow interaction with the layer tree in large projects
  • Loading branch information
nyalldawson committed Apr 9, 2021
1 parent 7126faf commit 881078f4221d3f58eaf91109e09cf6592ce46414
Showing with 9 additions and 4 deletions.
  1. +2 −1 src/core/labeling/qgspallabeling.cpp
  2. +5 −2 src/core/qgsapplication.cpp
  3. +2 −1 src/core/textrenderer/qgstextformat.cpp
@@ -1312,7 +1312,8 @@ QPixmap QgsPalLayerSettings::labelSettingsPreviewPixmap( const QgsPalLayerSettin
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
const double logicalDpiX = QgsApplication::desktop()->logicalDpiX();
#else
const double logicalDpiX = QApplication::topLevelWidgets().first()->screen()->logicalDotsPerInchX();
QWidget *activeWindow = QApplication::activeWindow();
const double logicalDpiX = activeWindow && activeWindow->screen() ? activeWindow->screen()->logicalDotsPerInchX() : 96.0;
#endif
context.setScaleFactor( logicalDpiX / 25.4 );

@@ -1865,8 +1865,11 @@ int QgsApplication::scaleIconSize( int standardSize, bool applyDevicePixelRatio
if ( applyDevicePixelRatio && QApplication::desktop() )
scaledIconSize *= QApplication::desktop()->devicePixelRatio();
#else
if ( applyDevicePixelRatio && !QApplication::topLevelWidgets().isEmpty() )
scaledIconSize *= QApplication::topLevelWidgets().first()->screen()->devicePixelRatio();
if ( applyDevicePixelRatio )
{
if ( QWidget *activeWindow = QApplication::activeWindow() )
scaledIconSize *= ( activeWindow->screen() ? QApplication::activeWindow()->screen()->devicePixelRatio() : 1 );
}
#endif
return scaledIconSize;
}
@@ -963,7 +963,8 @@ QPixmap QgsTextFormat::textFormatPreviewPixmap( const QgsTextFormat &format, QSi
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
const double logicalDpiX = QgsApplication::desktop()->logicalDpiX();
#else
const double logicalDpiX = QApplication::topLevelWidgets().first()->screen()->logicalDotsPerInchX();
QWidget *activeWindow = QApplication::activeWindow();
const double logicalDpiX = activeWindow && activeWindow->screen() ? activeWindow->screen()->logicalDotsPerInchX() : 96.0;
#endif
context.setScaleFactor( logicalDpiX / 25.4 );

0 comments on commit 881078f

Please sign in to comment.