Skip to content

Commit f9e547c

Browse files
elpasonyalldawson
authored andcommittedSep 16, 2020
Expose QgsProject::setInstance to API
Fixes #38755 Needs forward porting to all active branches
1 parent a5a5c4d commit f9e547c

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed
 

‎python/core/auto_generated/qgsproject.sip.in

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ open within the main QGIS application.
3838
Returns the QgsProject singleton instance
3939
%End
4040

41+
static void setInstance( QgsProject *project );
42+
%Docstring
43+
Set the current project instance to ``project``
44+
45+
.. note::
46+
47+
this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request
48+
49+
.. seealso:: :py:func:`instance`
50+
51+
.. versionadded:: 3.10
52+
%End
53+
54+
4155
explicit QgsProject( QObject *parent /TransferThis/ = 0 );
4256
%Docstring
4357
Create a new QgsProject.

‎src/core/qgsproject.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
108108
//! Returns the QgsProject singleton instance
109109
static QgsProject *instance();
110110

111+
/**
112+
* Set the current project instance to \a project
113+
*
114+
* \note this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request
115+
* \see instance()
116+
* \since QGIS 3.10.11
117+
*/
118+
static void setInstance( QgsProject *project ) ;
119+
120+
111121
/**
112122
* Create a new QgsProject.
113123
*
@@ -1651,15 +1661,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
16511661

16521662
static QgsProject *sProject;
16531663

1654-
/**
1655-
* Set the current project instance to \a project
1656-
*
1657-
* \note this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request
1658-
* \see instance()
1659-
* \note not available in Python bindings
1660-
* \since QGIS 3.2
1661-
*/
1662-
static void setInstance( QgsProject *project ) SIP_SKIP;
16631664

16641665
/**
16651666
* Read map layers from project file.

‎tests/src/python/test_qgsproject.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,14 @@ def testMapScales(self):
13601360
p.setUseProjectScales(False)
13611361
self.assertEqual(len(spy), 4)
13621362

1363+
def testSetInstance(self):
1364+
"""Test singleton API"""
1365+
1366+
p = QgsProject()
1367+
self.assertNotEqual(p, QgsProject.instance())
1368+
QgsProject.setInstance(p)
1369+
self.assertEqual(p, QgsProject.instance())
1370+
13631371

13641372
if __name__ == '__main__':
13651373
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.