pyqtqt5qgiscoordinate-systemspyqgis

How to let user select a CRS in QGIS Python plugin?


I am currently developing a QGIS Plugin in python. In it, the user should be able to select a Coordinate Reference System (CRS). In QGIS built-in dialogs, there often is this window called "Coordinate Reference System Selector" which allows selection of recently used CRS, as well as other Systems and also a Information box and visualisation of the Bbox of the CRS.

CRS Selector

Is there a way to replicate this? It would be very tideous to recreate all this informations and functionality. I would have excpected at least something CRS related in "QGIS custom widgets" in QT Designer, but I couldn't find anything.

Or if this isn't possible, is there at least a way to get all CRS's to display them in a Combobox?


Solution

  • You should be able to use QgsProjectionSelectionDialog:

    from qgis.gui import QgsProjectionSelectionDialog
    
    dialog = QgsProjectionSelectionDialog()
    dialog.exec_()
    
    crs = dialog.crs()
    
    print(crs)
    

    There is also QgsProjectionSelectionWidget if you want to embed it into your own dialog.