4

I am designing a GUI for a QGIS plugin I am developing, which is intended to plot information from a MySQL database. In the GUI, I would like to have a combo box of all the database connections saved in QGIS.

These can be viewed by printing allKeys() for QSettings() in PyQt4.QtCore. When this is done, the keys I am interested in are:

...
MySQL/connections/adsb/database
MySQL/connections/adsb/host
MySQL/connections/adsb/password
MySQL/connections/adsb/port
MySQL/connections/adsb/save
MySQL/connections/adsb/username
MySQL/connections/database
MySQL/connections/host
MySQL/connections/password
MySQL/connections/port
MySQL/connections/save
MySQL/connections/selected
MySQL/connections/strath_adsb/database
MySQL/connections/strath_adsb/host
MySQL/connections/strath_adsb/password
MySQL/connections/strath_adsb/port
MySQL/connections/strath_adsb/save
MySQL/connections/strath_adsb/username
...

Where 'adsb' and 'strath_adsb' are databases. How would it be possible to read the names of all connected databases and store them in an array, so that they could then be displayed as options in the combo box?

1
  • 1
    If the answer posted solved your problem, upvote it and mark it as accepted so this question can be considered closed. See tour: gis.stackexchange.com/tour Commented Apr 20, 2017 at 19:57

1 Answer 1

5

You can do it in this way:

from PyQt4.QtCore import QSettings

self.settings = QSettings()
self.settings.beginGroup( '/MySQL/connections/' )
connections = self.settings.childGroups() # [u'adsb', u'strath_adsb']
self.settings.endGroup()

self.myComboBox.addItems( connections )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.