[QGIS-Developer] How to access QgisApp::instance() from a core class

Nyall Dawson nyall.dawson at gmail.com
Sun Mar 6 15:03:33 PST 2022


On Sun, 6 Mar 2022 at 00:16, Schmetzer, Tobias via QGIS-Developer
<qgis-developer at lists.osgeo.org> wrote:
>
> Hello everyone,
>
>
> I would like to access QgisApp::instance() (src/app/qgisapp.cpp) from a provider like QgsOgrProvider::extent() (src/core/providers/ogr/qgsogrprovider.cpp.

You can't, by design.

CORE classes must be completely non-gui, so that they work without
issue on non-gui platforms like QGIS server (also for clients which
have their own GUI, such as QFIeld/Input).

Just to be super-blunt: There is 100% no way this will EVER change! ;)


> When looking for this information I found some inactivating preprocessor code block comments like this one (qgswfsdataitems.cpp):
>
> #if 0
>
>     // TODO: how to emit message from provider (which does not know about QgisApp)
>     QgisApp::instance()->messageBar()->pushMessage( tr( "Cannot copy style" ),
>         errorMsg,
>         Qgis::MessageLevel::Critical, messageTimeout() );
> #endif

This is very old code (hence why it's disabled). Again, none of the
core classes are allowed any GUI component, and the correct approach
to handle this particular situation is to move that code from the
provider level up to app level, specifically via the
QgsDataItemGuiProvider class.

So my question would be: why do you need to raise a user-facing
message from QgsOgrProvider::extent?

Nyall


More information about the QGIS-Developer mailing list