Skip to content

Commit ff786f2

Browse files
committedDec 4, 2018
Add group method to QgsSettings
Is present in the QSettings API, but missing from QgsSettings
1 parent a5bad2d commit ff786f2

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed
 

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ are based on the group. By default, no group is set.
142142
%Docstring
143143
Resets the group to what it was before the corresponding beginGroup() call.
144144
%End
145+
146+
QString group() const;
147+
%Docstring
148+
Returns the current group.
149+
150+
.. seealso:: :py:func:`beginGroup`
151+
152+
.. seealso:: :py:func:`endGroup`
153+
154+
.. versionadded:: 3.6
155+
%End
156+
145157
QStringList allKeys() const;
146158
%Docstring
147159
Returns a list of all keys, including subkeys, that can be read using the QSettings object.

‎src/core/qgssettings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ void QgsSettings::endGroup()
102102
}
103103
}
104104

105+
QString QgsSettings::group() const
106+
{
107+
return mUserSettings->group();
108+
}
105109

106110
QStringList QgsSettings::allKeys() const
107111
{

‎src/core/qgssettings.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ class CORE_EXPORT QgsSettings : public QObject
154154
void beginGroup( const QString &prefix, QgsSettings::Section section = QgsSettings::NoSection );
155155
//! Resets the group to what it was before the corresponding beginGroup() call.
156156
void endGroup();
157+
158+
/**
159+
* Returns the current group.
160+
* \see beginGroup()
161+
* \see endGroup()
162+
* \since QGIS 3.6
163+
*/
164+
QString group() const;
165+
157166
//! Returns a list of all keys, including subkeys, that can be read using the QSettings object.
158167
QStringList allKeys() const;
159168
//! Returns a list of all top-level keys that can be read using the QSettings object.

‎tests/src/python/test_qgssettings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def test_groups(self):
181181
self.addToDefaults('testqgissettings/name', 'qgisrocks')
182182

183183
self.settings.beginGroup('testqgissettings')
184+
self.assertEqual(self.settings.group(), 'testqgissettings')
184185
self.assertEqual(['names'], self.settings.childGroups())
185186

186187
self.settings.setValue('surnames/name1', 'qgisrocks-1')
@@ -189,11 +190,14 @@ def test_groups(self):
189190
self.settings.setValue('names/name1', 'qgisrocks-1')
190191
self.assertEqual('qgisrocks-1', self.settings.value('names/name1'))
191192
self.settings.endGroup()
193+
self.assertEqual(self.settings.group(), '')
192194
self.settings.beginGroup('testqgissettings/names')
195+
self.assertEqual(self.settings.group(), 'testqgissettings/names')
193196
self.settings.setValue('name4', 'qgisrocks-4')
194197
keys = sorted(self.settings.childKeys())
195198
self.assertEqual(keys, ['name1', 'name2', 'name3', 'name4'])
196199
self.settings.endGroup()
200+
self.assertEqual(self.settings.group(), '')
197201
self.assertEqual('qgisrocks-1', self.settings.value('testqgissettings/names/name1'))
198202
self.assertEqual('qgisrocks-4', self.settings.value('testqgissettings/names/name4'))
199203

@@ -205,9 +209,11 @@ def test_global_groups(self):
205209
self.addToDefaults('testqgissettings/foo/last', 'rocks')
206210

207211
self.settings.beginGroup('testqgissettings')
212+
self.assertEqual(self.settings.group(), 'testqgissettings')
208213
self.assertEqual(['foo'], self.settings.childGroups())
209214
self.assertEqual(['foo'], self.settings.globalChildGroups())
210215
self.settings.endGroup()
216+
self.assertEqual(self.settings.group(), '')
211217

212218
self.settings.setValue('testqgissettings/bar/first', 'qgis')
213219
self.settings.setValue('testqgissettings/bar/last', 'rocks')
@@ -227,6 +233,7 @@ def test_global_groups(self):
227233
def test_group_section(self):
228234
# Test group by using Section
229235
self.settings.beginGroup('firstgroup', section=QgsSettings.Core)
236+
self.assertEqual(self.settings.group(), 'core/firstgroup')
230237
self.assertEqual([], self.settings.childGroups())
231238
self.settings.setValue('key', 'value')
232239
self.settings.setValue('key2/subkey1', 'subvalue1')
@@ -237,6 +244,7 @@ def test_group_section(self):
237244
self.assertEqual(['key', 'key3'], self.settings.childKeys())
238245
self.assertEqual(['key2'], self.settings.childGroups())
239246
self.settings.endGroup()
247+
self.assertEqual(self.settings.group(), '')
240248
# Set value by writing the group manually
241249
self.settings.setValue('firstgroup/key4', 'value4', section=QgsSettings.Core)
242250
# Checking the value that have been set

0 commit comments

Comments
 (0)
Please sign in to comment.