Geography Reference
In-Depth Information
don't need to change the default actions of the OK and Cancel buttons.
Once we have all the controls on the form, we're ready to generate some
code from it.
To convert our dialog box (which we saved as
zoomtopointdialog.ui
) to
Python, we use the PyQt
pyuic4
command to compile it:
pyuic4 -o ui_zoomtopoint.py zoomtopointdialog.ui
This gives us
ui_zoomtopoint.py
containing the code necessary to create
the dialog box when the plugin is launched. There is one more thing we
need to get the dialog box up on the screen. We need a bit of code that
imports the user interface and displays the form. For this we create
zoomtopointdialog.py
containing the following:
from
PyQt4
import
QtCore, QtGui
from
ui_zoomtopoint
import
Ui_ZoomToPoint
class
ZoomToPointDialog(QtGui.QDialog):
def
__init__(self):
QtGui.QDialog.__init__(self)
# Set up the user interface from Designer.
self.ui = Ui_ZoomToPoint()
self.ui.setupUi(self)
This bit of code uses the
ui_zoomtopoint.py
script in
setupUi(self)
to set
things up. Our GUI is now ready for use. All we need to write now is
the Python code to interact with the QGIS map canvas.
Getting to Zoom
We're now ready to write the actual code that does something with the
map. Up to this point we've just been getting the plumbing put in. Now
we'll write the code to actually zoom to the point we enter in our dialog
box. As we go, we'll look at the code in chunks to make it a bit easier.
Let's start by looking at the things we need to import and the initializa-
tion of the plugin:
# Import the PyQt and QGIS libraries
Line 1
from
PyQt4.QtCore
import
*
-
from
PyQt4.QtGui
import
*
-
from
qgis.core
import
*
-
# Initialize Qt resources from file resources.py
5
import
resources
-
# Import the code for the dialog
-
from
zoomtopointdialog
import
ZoomToPointDialog
-
-
Search WWH ::
Custom Search