Re: [Qgis-developer] python application
Hi, On 06/07/2013 08:34 AM, JBE wrote: Hello, I'm trying to create a pygis-application with a code found in the "quantum gis coding and application guide". I've programmed before but I'm new to python as such, so right now I'm basically just trying to break this down and understand how it works. To the issue: I'm using the code below but I get the "invalid syntax" error on the line of: self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer) where "..actionAddLayer" is marked. try self.actionAddLayer.activated.connect(self.addLayer) What is wrong here? cheers -- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * import sys import os # Import our GUI from mainwindow_ui import Ui_MainWindow # Environment variable QGISHOME must be set to the 1.0 install directory # before running this application qgis_prefix = os.getenv("C:\Program Files\Quantum GIS Lisboa") class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) # Required by Qt4 to initialize the UI self.setupUi(self) # Set the title for the app self.setWindowTitle("QGIS Demo App") # Create the map canvas self.canvas = QgsMapCanvas() # Set the background color to light blue something self.canvas.setCanvasColor(QColor(200,200,255)) self.canvas.enableAntiAliasing(True) self.canvas.useQImageToRender(False) self.canvas.show() # Lay our widgets out in the main window using a # vertical box layout self.layout = QVBoxLayout(self.frame) self.layout.addWidget(self.canvas) # Create the actions for our tools and connect each to the appropriate # method self.actionAddLayer = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionAddLayer.png"), "Add Layer", self.frame) self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer) self.actionZoomIn = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomIn.png"), \ "Zoom In", self.frame) self.connect(self.actionZoomIn, SIGNAL("activated()"), self.zoomIn) self.actionZoomOut = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomOut.png"), \ "Zoom Out", self.frame) self.connect(self.actionZoomOut, SIGNAL("activated()"), self.zoomOut) self.actionPan = QAction(QIcon("(qgis_prefix + \"/share/qgis/themes/classic/mActionPan.png"), \ "Pan", self.frame) self.connect(self.actionPan, SIGNAL("activated()"), self.pan) self.actionZoomFull = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomFullExtent.png"), \ "Zoom Full Extent", self.frame) self.connect(self.actionZoomFull, SIGNAL("activated()"), self.zoomFull) # Create a toolbar self.toolbar = self.addToolBar("Map") # Add the actions to the toolbar self.toolbar.addAction(self.actionAddLayer) self.toolbar.addAction(self.actionZoomIn) self.toolbar.addAction(self.actionZoomOut); self.toolbar.addAction(self.actionPan); self.toolbar.addAction(self.actionZoomFull); # Create the map tools self.toolPan = QgsMapToolPan(self.canvas) self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out # Set the map tool to zoom in def zoomIn(self): self.canvas.setMapTool(self.toolZoomIn) # Set the map tool to zoom out def zoomOut(self): self.canvas.setMapTool(self.toolZoomOut) # Set the map tool to def pan(self): self.canvas.setMapTool(self.toolPan) # Zoom to full extent of layer def zoomFull(self): self.canvas.zoomFullExtent() # Add an OGR layer to the map def addLayer(self): file = QFileDialog.getOpenFileName(self, "Open Shapefile", ".", "Shapefiles (*.shp)") fileInfo = QFileInfo(file) # Add the layer layer = QgsVectorLayer(file, fileInfo.fileName(), "ogr") if not layer.isValid(): return # Change the color of the layer to gray symbols = layer.renderer().symbols() symbol = symbols[0] symbol.setFillColor(QColor.fromRgb(192,192,192)) # Add layer to the registry QgsMapLayerRegistry.instance().addMapLayer(layer); # Set extent to the extent of our layer self.canvas.setExtent(layer.extent()) # Set up the map canvas layer set cl = QgsMapCanvasLayer(layer) layers = [cl] self.canvas.setLayerSet(layers) def main(argv): # create Qt application app = QApplication(argv)
[Qgis-developer] python application
Hello, I'm trying to create a pygis-application with a code found in the "quantum gis coding and application guide". I've programmed before but I'm new to python as such, so right now I'm basically just trying to break this down and understand how it works. To the issue: I'm using the code below but I get the "invalid syntax" error on the line of: self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer) where "..actionAddLayer" is marked. What is wrong here? cheers -- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * import sys import os # Import our GUI from mainwindow_ui import Ui_MainWindow # Environment variable QGISHOME must be set to the 1.0 install directory # before running this application qgis_prefix = os.getenv("C:\Program Files\Quantum GIS Lisboa") class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) # Required by Qt4 to initialize the UI self.setupUi(self) # Set the title for the app self.setWindowTitle("QGIS Demo App") # Create the map canvas self.canvas = QgsMapCanvas() # Set the background color to light blue something self.canvas.setCanvasColor(QColor(200,200,255)) self.canvas.enableAntiAliasing(True) self.canvas.useQImageToRender(False) self.canvas.show() # Lay our widgets out in the main window using a # vertical box layout self.layout = QVBoxLayout(self.frame) self.layout.addWidget(self.canvas) # Create the actions for our tools and connect each to the appropriate # method self.actionAddLayer = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionAddLayer.png"), "Add Layer", self.frame) self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer) self.actionZoomIn = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomIn.png"), \ "Zoom In", self.frame) self.connect(self.actionZoomIn, SIGNAL("activated()"), self.zoomIn) self.actionZoomOut = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomOut.png"), \ "Zoom Out", self.frame) self.connect(self.actionZoomOut, SIGNAL("activated()"), self.zoomOut) self.actionPan = QAction(QIcon("(qgis_prefix + \"/share/qgis/themes/classic/mActionPan.png"), \ "Pan", self.frame) self.connect(self.actionPan, SIGNAL("activated()"), self.pan) self.actionZoomFull = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomFullExtent.png"), \ "Zoom Full Extent", self.frame) self.connect(self.actionZoomFull, SIGNAL("activated()"), self.zoomFull) # Create a toolbar self.toolbar = self.addToolBar("Map") # Add the actions to the toolbar self.toolbar.addAction(self.actionAddLayer) self.toolbar.addAction(self.actionZoomIn) self.toolbar.addAction(self.actionZoomOut); self.toolbar.addAction(self.actionPan); self.toolbar.addAction(self.actionZoomFull); # Create the map tools self.toolPan = QgsMapToolPan(self.canvas) self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out # Set the map tool to zoom in def zoomIn(self): self.canvas.setMapTool(self.toolZoomIn) # Set the map tool to zoom out def zoomOut(self): self.canvas.setMapTool(self.toolZoomOut) # Set the map tool to def pan(self): self.canvas.setMapTool(self.toolPan) # Zoom to full extent of layer def zoomFull(self): self.canvas.zoomFullExtent() # Add an OGR layer to the map def addLayer(self): file = QFileDialog.getOpenFileName(self, "Open Shapefile", ".", "Shapefiles (*.shp)") fileInfo = QFileInfo(file) # Add the layer layer = QgsVectorLayer(file, fileInfo.fileName(), "ogr") if not layer.isValid(): return # Change the color of the layer to gray symbols = layer.renderer().symbols() symbol = symbols[0] symbol.setFillColor(QColor.fromRgb(192,192,192)) # Add layer to the registry QgsMapLayerRegistry.instance().addMapLayer(layer); # Set extent to the extent of our layer self.canvas.setExtent(layer.extent()) # Set up the map canvas layer set cl = QgsMapCanvasLayer(layer) layers = [cl] self.canvas.setLayerSet(layers) def main(argv): # create Qt application app = QApplication(argv) # Initialize qgis libraries QgsApplication.setPrefixPath(qgis_prefix, True) QgsApplication.initQgis() # create main window wnd = MainWindow()