diff --git a/python/core/auto_generated/qgsproject.sip.in b/python/core/auto_generated/qgsproject.sip.in index 3731bcabe7e..8dc1c96a231 100644 --- a/python/core/auto_generated/qgsproject.sip.in +++ b/python/core/auto_generated/qgsproject.sip.in @@ -69,6 +69,20 @@ open within the main QGIS application. Returns the QgsProject singleton instance %End + static void setInstance( QgsProject *project ); +%Docstring +Set the current project instance to ``project`` + +.. note:: + + this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request + +.. seealso:: :py:func:`instance` + +.. versionadded:: 3.10 +%End + + explicit QgsProject( QObject *parent /TransferThis/ = 0 ); %Docstring Create a new QgsProject. diff --git a/src/core/qgsproject.h b/src/core/qgsproject.h index 4661b158783..93151bd6ac1 100644 --- a/src/core/qgsproject.h +++ b/src/core/qgsproject.h @@ -165,6 +165,16 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera //! Returns the QgsProject singleton instance static QgsProject *instance(); + /** + * Set the current project instance to \a project + * + * \note this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request + * \see instance() + * \since QGIS 3.10.11 + */ + static void setInstance( QgsProject *project ) ; + + /** * Create a new QgsProject. * @@ -1837,15 +1847,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera static QgsProject *sProject; - /** - * Set the current project instance to \a project - * - * \note this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request - * \see instance() - * \note not available in Python bindings - * \since QGIS 3.2 - */ - static void setInstance( QgsProject *project ) SIP_SKIP; /** * Read map layers from project file. diff --git a/tests/src/python/test_qgsproject.py b/tests/src/python/test_qgsproject.py index 59a76e970c4..fd4c0f239d5 100644 --- a/tests/src/python/test_qgsproject.py +++ b/tests/src/python/test_qgsproject.py @@ -1360,6 +1360,14 @@ class TestQgsProject(unittest.TestCase): p.setUseProjectScales(False) self.assertEqual(len(spy), 4) + def testSetInstance(self): + """Test singleton API""" + + p = QgsProject() + self.assertNotEqual(p, QgsProject.instance()) + QgsProject.setInstance(p) + self.assertEqual(p, QgsProject.instance()) + if __name__ == '__main__': unittest.main()