[PyQt] How to show maps in GUI and get coordinates with clicks

Rabson, Hugo SPC MIL USA USARPAC hugo.rabson at us.army.mil
Tue Oct 6 02:35:59 BST 2009


"Echavarria Gregory, Maria Angelica" <m.echavarriagregory at umiami.edu> wrote:-
> I have my working GUI programmed in PyQt. Now I need to:
> 1. show a map (raster image) as foreground in my QGraphicsView with 
> a specified coordinate pair in the middle of the QGraphicsView,
> and more important:
> 2. be able to pass sets of coordinates when users click on a point 
> of the map
> What is the simplest way of doing this? any examples?
> Does the GDAL library for Python have some use in this PyQt 
> application?

I've some suggestions. However, be advised that I am a novice.

Try this: Create a QGraphicsView widget, create a custom subclass of QGraphicsScene, use QGraphicsVIew.setScene() to make the graphicsview use your graphicsscene, and then add a pixmap of your drawing (map or whatever) to the graphicsscene. Finally, add some code to your graphicsscene to detect and respond to mouse-clicks.

The more complex route would be to create a QGraphicsView widget, create a custom subclasss of QGraphicsScene (to catch mouse-clicks that *define* the hotspots), then populate the graphicsscene with lots of hotspots.

Either way, you can use the same form for *defining* clickable locations and for *detecting* those locations. Just vary the parameters (add an 'editable=False' or 'editable=True'). That will save you a bit of trouble. :) Display the map class once with editing enabled, define the hot-spots by clicking on the map, save the data table you've created, and then call the map class again with editing disabled (to test the map class).

If you would like to see the code I've been working on, please download this snapshot:-
# svn co https://usanaomi.svn.sourceforge.net/svnroot/usanaomi usanaomi
# cd usanaomi/trunk/src
# python test-technicalmanual.py

...and check out the following source files:-
a) test-technicalmanual.py and ui/TechnicalManualDialog.py
b) utilities/gui.py
c) utilities/hotspotstuff.py

The code is not well-written, and the download is ridiculously large at present, but my goal is similar to yours.

-Hugo Rabson


More information about the PyQt mailing list