QgsApplication class

Extends QApplication to provide access to QGIS specific resources such as theme paths, database paths etc.

This is a subclass of QApplication and should be instantiated in place of QApplication. Most methods are static in keeping with the design of QApplication.

This class hides platform-specific path information and provides a portable way of referencing specific files and directories. Ideally, hard-coded paths should appear only here and not in other modules so that platform-conditional code is minimized and paths are easier to change due to centralization.

Public types

enum Cursor { ZoomIn, ZoomOut, Identify, CrossHair, CapturePoint, Select, Sampler }
The Cursor enum defines constants for QGIS custom cursors.
enum endian_t { XDR = 0, NDR = 1 }
Constants for endian-ness.

Public static functions

static auto absolutePathToRelativePath(const QString& apath, const QString& targetPath) -> QString
Converts absolute path to path relative to target.
static auto actionScopeRegistry() -> QgsActionScopeRegistry*
Returns the action scope registry.
static auto activeThemePath() -> QString
Returns the path to the currently active theme directory.
static auto annotationRegistry() -> QgsAnnotationRegistry*
Returns the application's annotation registry, used for managing annotation types.
static auto appIconPath() -> QString
Gets application icon.
static void applyGdalSkippedDrivers()
Apply the skipped drivers list to gdal.
static auto authManager() -> QgsAuthManager*
Returns the application's authentication manager instance.
static auto authorsFilePath() -> QString
Returns the path to the authors file.
static auto buildOutputPath() -> QString
Returns path to the build output directory. Valid only when running from build directory.
static auto buildSourcePath() -> QString
Returns path to the source directory. Valid only when running from build directory.
static auto colorSchemeRegistry() -> QgsColorSchemeRegistry*
Returns the application's color scheme registry, used for managing color schemes.
static auto contributorsFilePath() -> QString
Returns the path to the contributors file.
static auto createDatabase(QString* errorMessage = nullptr) -> bool
initialize qgis.db
static auto createThemeFolder() -> bool
Create the users theme folder.
static auto customVariables() -> QVariantMap
Custom expression variables for this application.
static auto dataItemProviderRegistry() -> QgsDataItemProviderRegistry*
Returns the application's data item provider registry, which keeps a list of data item providers that may add items to the browser tree.
static auto defaultStylePath() -> QString
Returns the path to default style (works as a starting point).
static auto defaultThemePath() -> QString
Returns the path to the default theme directory.
static auto defaultThemesFolder() -> QString
Returns the path to default themes folder from install (works as a starting point).
static auto developersMapFilePath() -> QString
Returns the path to the developers map file.
static auto donorsFilePath() -> QString
Returns the path to the donors file.
static auto endian() -> endian_t
Returns whether this machine uses big or little endian.
template<typename T>
static void endian_swap(T& value)
Swap the endianness of the specified value.
static void exitQgis()
deletes provider registry and map layer registry
static auto fieldFormatterRegistry() -> QgsFieldFormatterRegistry*
Gets the registry of available field formatters.
static auto getThemeCursor(Cursor cursor) -> QCursor
Helper to get a theme cursor.
static auto getThemeIcon(const QString& name) -> QIcon
Helper to get a theme icon.
static auto getThemePixmap(const QString& name) -> QPixmap
Helper to get a theme icon as a pixmap.
static auto gpsConnectionRegistry() -> QgsGpsConnectionRegistry*
Returns the application's GPS connection registry, used for managing GPS connections.
static auto i18nPath() -> QString
Returns the path to the translation directory.
static auto iconPath(const QString& iconFile) -> QString
Returns path to the desired icon file.
static auto iconsPath() -> QString
Returns the path to the icons image directory.
static auto imageCache() -> QgsImageCache*
Returns the application's image cache, used for caching resampled versions of raster images.
static void init(QString profileFolder = QString())
This method initializes paths etc for QGIS.
static void initQgis()
loads providers
static auto instance() -> QgsApplication*
Returns the singleton instance of the QgsApplication.
static auto isRunningFromBuildDir() -> bool
Indicates whether running from build directory (not installed)
static auto layoutItemRegistry() -> QgsLayoutItemRegistry*
Returns the application's layout item registry, used for layout item types.
static auto layoutTemplatePaths() -> QStringList
Returns the paths to layout template directories.
static auto libexecPath() -> QString
Returns the path with utility executables (help viewer, crssync, ...)
static auto libraryPath() -> QString
Returns the path containing qgis_core, qgis_gui, qgispython (and other) libraries.
static auto licenceFilePath() -> QString
Returns the path to the licence file.
static auto locale() -> QString
Returns the QGIS locale.
static auto maxThreads() -> int
Gets maximum concurrent thread count.
static auto messageLog() -> QgsMessageLog*
Returns the application's message log.
static auto metadataPath() -> QString
Returns the path to the metadata directory.
static auto networkContentFetcherRegistry() -> QgsNetworkContentFetcherRegistry*
Returns the application's network content registry used for fetching temporary files during QGIS session.
static auto nullRepresentation() -> QString
This string is used to represent the value NULL throughout QGIS.
static auto osName() -> QString
Returns a string name of the operating system QGIS is running on.
static auto pageSizeRegistry() -> QgsPageSizeRegistry*
Returns the application's page size registry, used for managing layout page sizes.
static auto paintEffectRegistry() -> QgsPaintEffectRegistry*
Returns the application's paint effect registry, used for managing paint effects.
static auto pkgDataPath() -> QString
Returns the common root path of all application data directories.
static auto platform() -> QString
Returns the QGIS platform name, e.g., "desktop" or "server".
static auto pluginLayerRegistry() -> QgsPluginLayerRegistry*
Returns the application's plugin layer registry, used for managing plugin layer types.
static auto pluginPath() -> QString
Returns the path to the application plugin directory.
static auto prefixPath() -> QString
Returns the path to the application prefix directory.
static auto processingRegistry() -> QgsProcessingRegistry*
Returns the application's processing registry, used for managing processing providers, algorithms, and various parameters and outputs.
static auto profiler() -> QgsRuntimeProfiler*
Returns the application runtime profiler.
static auto projectStorageRegistry() -> QgsProjectStorageRegistry*
Returns registry of available project storage implementations.
static auto qgisAuthDatabaseFilePath() -> QString
Returns the path to the user authentication database file: qgis-auth.db.
static auto qgisMasterDatabaseFilePath() -> QString
Returns the path to the master qgis.db file.
static auto qgisSettingsDirPath() -> QString
Returns the path to the settings directory in user's home dir.
static auto qgisUserDatabaseFilePath() -> QString
Returns the path to the user qgis.db file.
static auto qmlImportPath() -> QString
Returns the path where QML components are installed for QGIS Quick library.
static auto rasterRendererRegistry() -> QgsRasterRendererRegistry*
Returns the application's raster renderer registry, used for managing raster layer renderers.
static void registerOgrDrivers()
Register OGR drivers ensuring this only happens once.
static auto relativePathToAbsolutePath(const QString& rpath, const QString& targetPath) -> QString
Converts path relative to target to an absolute path.
static auto renderer3DRegistry() -> Qgs3DRendererRegistry*
Returns registry of available 3D renderers.
static auto rendererRegistry() -> QgsRendererRegistry*
Returns the application's renderer registry, used for managing vector layer renderers.
static auto reportStyleSheet() -> QString
Returns a standard css style sheet for reports.
static auto resolvePkgPath() -> QString
Calculate the application pkg path.
static void restoreGdalDriver(const QString& driver)
Sets the GDAL_SKIP environment variable to exclude the specified driver and then calls GDALDriverManager::AutoSkipDrivers() to unregister it.
static auto serverResourcesPath() -> QString
Returns the path to the server resources directory.
static void setAuthDatabaseDirPath(const QString& authDbDirPath)
Alters authentication data base directory path - used by 3rd party apps.
static void setCustomVariable(const QString& name, const QVariant& value)
Set a single custom expression variable.
static void setCustomVariables(const QVariantMap& customVariables)
Custom expression variables for this application.
static void setDefaultSvgPaths(const QStringList& pathList)
Alters default svg paths - used by 3rd party apps.
static void setFileOpenEventReceiver(QObject* receiver)
Sets the FileOpen event receiver.
static void setMaxThreads(int maxThreads)
Set maximum concurrent thread count.
static void setNullRepresentation(const QString& nullRepresentation)
This string is used to represent the value NULL throughout QGIS.
static void setPkgDataPath(const QString& pkgDataPath)
Alters pkg data path - used by 3rd party apps.
static void setPluginPath(const QString& pluginPath)
Alters plugin path - used by 3rd party apps.
static void setPrefixPath(const QString& prefixPath, bool useDefaultPaths = false)
Alters prefix path - used by 3rd party apps.
static void setThemeName(const QString& themeName)
Set the active theme to the specified theme.
static void setTranslation(const QString& translation)
Set translation.
static void setUITheme(const QString& themeName)
Set the current UI theme used to style the interface.
static auto shortNameRegExp() -> QRegExp
Returns the short name regular expression for line edit validator.
static auto showSettings() -> QString
Convenience function to get a summary of the paths used in this application instance useful for debugging mainly.
static void skipGdalDriver(const QString& driver)
Sets the GDAL_SKIP environment variable to include the specified driver and then calls GDALDriverManager::AutoSkipDrivers() to unregister it.
static auto skippedGdalDrivers() -> QStringList
Returns the list of gdal drivers that should be skipped (based on GDAL_SKIP environment variable)
static auto splashPath() -> QString
Returns the path to the splash screen image directory.
static auto sponsorsFilePath() -> QString
Returns the path to the sponsors file.
static auto srsDatabaseFilePath() -> QString
Returns the path to the srs.db file.
static auto svgCache() -> QgsSvgCache*
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement within SVG files.
static auto svgPaths() -> QStringList
Returns the paths to svg directories.
static auto symbolLayerRegistry() -> QgsSymbolLayerRegistry*
Returns the application's symbol layer registry, used for managing symbol layers.
static auto systemEnvVars() -> QMap<QString, QString>
Returns the system environment variables passed to application.
static auto taskManager() -> QgsTaskManager*
Returns the application's task manager, used for managing application wide background task handling.
static auto themeName() -> QString
Set the active theme to the specified theme.
static auto translatorsFilePath() -> QString
Returns the path to the sponsors file.
static auto uiThemes() -> QHash<QString, QString>
All themes found in ~/.qgis3/themes folder.
static auto userFullName() -> QString
Returns the user's operating system login account full display name.
static auto userLoginName() -> QString
Returns the user's operating system login account name.
static auto userStylePath() -> QString
Returns the path to user's style.
static auto userThemesFolder() -> QString
Returns the path to user's themes folder.
static auto validityCheckRegistry() -> QgsValidityCheckRegistry*
Returns the application's validity check registry, used for managing validity checks.

Public functions

void collectTranslatableObjects(QgsTranslationContext* translationContext)
Emits the signal to collect all the strings of .qgs to be included in ts file.
auto event(QEvent* event) -> bool override
Watch for QFileOpenEvent.
auto maxConcurrentConnectionsPerPool() const -> int
The maximum number of concurrent connections per connections pool.
auto notify(QObject* receiver, QEvent* event) -> bool override
Catch exceptions when sending event to receiver.

Signals

void customVariablesChanged()
Emitted whenever a custom global variable changes.
void nullRepresentationChanged()
This string is used to represent the value NULL throughout QGIS.
void preNotify(QObject* receiver, QEvent* event, bool* done)
void requestForTranslatableObjects(QgsTranslationContext* translationContext)
Emitted when project strings which require translation are being collected for inclusion in a .ts file.

Enum documentation

enum QgsApplication::Cursor

The Cursor enum defines constants for QGIS custom cursors.

Enumerators
ZoomIn

Zoom in.

ZoomOut

Zoom out.

Identify

Identify: obtain information about the object.

CrossHair

Precisely identify a point on the canvas.

CapturePoint

Select and capture a point or a feature.

Select

Select a rectangle.

Sampler

Color/Value picker.

Function documentation

static QgsActionScopeRegistry* QgsApplication::actionScopeRegistry()

Returns the action scope registry.

static QgsAnnotationRegistry* QgsApplication::annotationRegistry()

Returns the application's annotation registry, used for managing annotation types.

static void QgsApplication::applyGdalSkippedDrivers()

Apply the skipped drivers list to gdal.

static QgsAuthManager* QgsApplication::authManager()

Returns the application's authentication manager instance.

static QgsColorSchemeRegistry* QgsApplication::colorSchemeRegistry()

Returns the application's color scheme registry, used for managing color schemes.

static QString QgsApplication::contributorsFilePath()

Returns the path to the contributors file.

Contributors are people who have submitted patches but don't have commit access.

static QVariantMap QgsApplication::customVariables()

Custom expression variables for this application.

This does not include generated variables (like system name, user name etc.)

static QgsDataItemProviderRegistry* QgsApplication::dataItemProviderRegistry()

Returns the application's data item provider registry, which keeps a list of data item providers that may add items to the browser tree.

static QString QgsApplication::developersMapFilePath()

Returns the path to the developers map file.

The developers map was created by using leaflet framework, it shows the contributors.json file.

template<typename T>
static void QgsApplication::endian_swap(T& value)

Swap the endianness of the specified value.

static QCursor QgsApplication::getThemeCursor(Cursor cursor)

Helper to get a theme cursor.

It will fall back to the default theme if the active theme does not have the required icon. Cursors are automatically scaled to look like a 16px cursor on 96dpi screens.

static QIcon QgsApplication::getThemeIcon(const QString& name)

Helper to get a theme icon.

It will fall back to the default theme if the active theme does not have the required icon.

static QPixmap QgsApplication::getThemePixmap(const QString& name)

Helper to get a theme icon as a pixmap.

It will fall back to the default theme if the active theme does not have the required icon.

static QgsGpsConnectionRegistry* QgsApplication::gpsConnectionRegistry()

Returns the application's GPS connection registry, used for managing GPS connections.

static QString QgsApplication::iconPath(const QString& iconFile)

Returns path to the desired icon file.

First it tries to use the active theme path, then default theme path

static QgsImageCache* QgsApplication::imageCache()

Returns the application's image cache, used for caching resampled versions of raster images.

static void QgsApplication::init(QString profileFolder = QString())

This method initializes paths etc for QGIS.

Called by the ctor or call it manually when your app does not extend the QApplication class.

static QgsApplication* QgsApplication::instance()

Returns the singleton instance of the QgsApplication.

static QgsLayoutItemRegistry* QgsApplication::layoutItemRegistry()

Returns the application's layout item registry, used for layout item types.

static QStringList QgsApplication::layoutTemplatePaths()

Returns the paths to layout template directories.

static QString QgsApplication::locale()

Returns the QGIS locale.

static int QgsApplication::maxThreads()

Gets maximum concurrent thread count.

static QgsMessageLog* QgsApplication::messageLog()

Returns the application's message log.

static QString QgsApplication::metadataPath()

Returns the path to the metadata directory.

static QgsNetworkContentFetcherRegistry* QgsApplication::networkContentFetcherRegistry()

Returns the application's network content registry used for fetching temporary files during QGIS session.

static QString QgsApplication::nullRepresentation()

This string is used to represent the value NULL throughout QGIS.

In general, when passing values around, prefer to use a null QVariant QVariant( field.type() ) or QVariant( QVariant::Int ). This value should only be used in the final presentation step when showing values in a widget or sending it to a web browser.

static QString QgsApplication::osName()

Returns a string name of the operating system QGIS is running on.

static QgsPageSizeRegistry* QgsApplication::pageSizeRegistry()

Returns the application's page size registry, used for managing layout page sizes.

static QgsPaintEffectRegistry* QgsApplication::paintEffectRegistry()

Returns the application's paint effect registry, used for managing paint effects.

static QString QgsApplication::platform()

Returns the QGIS platform name, e.g., "desktop" or "server".

static QgsPluginLayerRegistry* QgsApplication::pluginLayerRegistry()

Returns the application's plugin layer registry, used for managing plugin layer types.

static QgsProcessingRegistry* QgsApplication::processingRegistry()

Returns the application's processing registry, used for managing processing providers, algorithms, and various parameters and outputs.

static QgsRuntimeProfiler* QgsApplication::profiler()

Returns the application runtime profiler.

static QgsProjectStorageRegistry* QgsApplication::projectStorageRegistry()

Returns registry of available project storage implementations.

static QString QgsApplication::qmlImportPath()

Returns the path where QML components are installed for QGIS Quick library.

Returns empty string when QGIS is built without Quick support

static QgsRasterRendererRegistry* QgsApplication::rasterRendererRegistry()

Returns the application's raster renderer registry, used for managing raster layer renderers.

static void QgsApplication::registerOgrDrivers()

Register OGR drivers ensuring this only happens once.

This is a workaround for an issue with older gdal versions that caused duplicate driver name entries to appear in the list of registered drivers when QgsApplication::registerOgrDrivers was called multiple times.

static Qgs3DRendererRegistry* QgsApplication::renderer3DRegistry()

Returns registry of available 3D renderers.

static QgsRendererRegistry* QgsApplication::rendererRegistry()

Returns the application's renderer registry, used for managing vector layer renderers.

static QString QgsApplication::reportStyleSheet()

Returns a standard css style sheet for reports.

Returns QString containing the CSS 2.1 compliant stylesheet.

Typically you will use this method by doing: QString myStyle = QgsApplication::reportStyleSheet(); textBrowserReport->document()->setDefaultStyleSheet(myStyle);

static QString QgsApplication::resolvePkgPath()

Calculate the application pkg path.

Returns the resolved pkg path

static void QgsApplication::restoreGdalDriver(const QString& driver)

Sets the GDAL_SKIP environment variable to exclude the specified driver and then calls GDALDriverManager::AutoSkipDrivers() to unregister it.

The driver name should be the short format of the Gdal driver name e.g. GTIFF.

static void QgsApplication::setCustomVariable(const QString& name, const QVariant& value)

Set a single custom expression variable.

static void QgsApplication::setCustomVariables(const QVariantMap& customVariables)

Custom expression variables for this application.

Do not include generated variables (like system name, user name etc.)

static void QgsApplication::setMaxThreads(int maxThreads)

Set maximum concurrent thread count.

static void QgsApplication::setNullRepresentation(const QString& nullRepresentation)

This string is used to represent the value NULL throughout QGIS.

In general, when passing values around, prefer to use a null QVariant QVariant( field.type() ) or QVariant( QVariant::Int ). This value should only be used in the final presentation step when showing values in a widget or sending it to a web browser.

static void QgsApplication::setThemeName(const QString& themeName)

Set the active theme to the specified theme.

The theme name should be a single word e.g. 'default','classic'. The theme search path usually will be pkgDataPath + "/themes/" + themName + "/" but plugin writers etc can use themeName() as a basis for searching for resources in their own datastores e.g. a Qt4 resource bundle.

static void QgsApplication::setTranslation(const QString& translation)

Set translation.

static void QgsApplication::setUITheme(const QString& themeName)

Set the current UI theme used to style the interface.

Parameters
themeName The name of the theme.

Use uiThemes() to find valid themes to use. Variables found in variables.qss will be added to the stylesheet on load.

static void QgsApplication::skipGdalDriver(const QString& driver)

Sets the GDAL_SKIP environment variable to include the specified driver and then calls GDALDriverManager::AutoSkipDrivers() to unregister it.

The driver name should be the short format of the Gdal driver name e.g. GTIFF.

static QgsSvgCache* QgsApplication::svgCache()

Returns the application's SVG cache, used for caching SVG images and handling parameter replacement within SVG files.

static QgsSymbolLayerRegistry* QgsApplication::symbolLayerRegistry()

Returns the application's symbol layer registry, used for managing symbol layers.

static QgsTaskManager* QgsApplication::taskManager()

Returns the application's task manager, used for managing application wide background task handling.

static QString QgsApplication::themeName()

Set the active theme to the specified theme.

The theme name should be a single word e.g. 'default','classic'. The theme search path usually will be pkgDataPath + "/themes/" + themName + "/" but plugin writers etc can use this method as a basis for searching for resources in their own datastores e.g. a Qt4 resource bundle.

static QHash<QString, QString> QgsApplication::uiThemes()

All themes found in ~/.qgis3/themes folder.

Returns A hash of theme name and theme path. Valid theme folders contain style.qss

The path is to the root folder for the theme

static QString QgsApplication::userFullName()

Returns the user's operating system login account full display name.

static QString QgsApplication::userLoginName()

Returns the user's operating system login account name.

static QgsValidityCheckRegistry* QgsApplication::validityCheckRegistry()

Returns the application's validity check registry, used for managing validity checks.

void QgsApplication::collectTranslatableObjects(QgsTranslationContext* translationContext)

Emits the signal to collect all the strings of .qgs to be included in ts file.

int QgsApplication::maxConcurrentConnectionsPerPool() const

The maximum number of concurrent connections per connections pool.

void QgsApplication::customVariablesChanged() signal

Emitted whenever a custom global variable changes.

void QgsApplication::nullRepresentationChanged() signal

This string is used to represent the value NULL throughout QGIS.

In general, when passing values around, prefer to use a null QVariant QVariant( field.type() ) or QVariant( QVariant::Int ). This value should only be used in the final presentation step when showing values in a widget or sending it to a web browser.

void QgsApplication::preNotify(QObject* receiver, QEvent* event, bool* done) signal

void QgsApplication::requestForTranslatableObjects(QgsTranslationContext* translationContext) signal

Emitted when project strings which require translation are being collected for inclusion in a .ts file.

In order to register translatable strings, connect to this signal and register the strings within the specified translationContext.