#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0

    Expected an indented block


    Hello,

    I have some programming background, but I'm totally new to python as such.

    I've come across a code from a tutorial I found online regarding how to build a simple mapping program with python and QGIS, PyQGIS. Currently I'm trying to use break this code down and understand it properly.

    As to my question, when I (using python IDLE) do a module check I get an error saying "expected an indented block". The program marks "def" as incorrect in the line of
    def __init__(self):

    which is right below the line of
    class MainWindow(QMainWindow, Ui_MainWindow):
    in the beginning of the code.

    What is wrong with that part? Why am I getting this error here? And how do I fix it? The full code is pasted below.

    Thank you

    ---------------------------------------------------------

    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()
    # Move the app window to upper left
    wnd.move(100,100)
    wnd.show()

    # run!
    retval = app.exec_()

    # exit
    QgsApplication.exitQgis()
    sys.exit(retval)

    if __name__ == "__main__":
    main(sys.argv)
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0
    Python code works withour brackets but with indentations. You must indent a code.
    Everything which is in class must be indeted as well as everything which is in function definition (def) or 'if', 'while'

    The part of your code should look like this:

    Code:
    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")
    You 'def __init__' is in class, so it must be indented. Of course you need to do indentation with spaces not with dashes.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Tewksbury, MA
    Posts
    31
    Rep Power
    2
    You MUST use code tags when you post python code or we can't see what might be wrong. Especially if the problem is indentation. Edit your post, select the code and press the # button.
    This does look like a possible mix-up of spaces and tabs as kdbxX mentioned.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Hello,

    Thank you for your help. I tabbed the code and then I got rid of the problem.

    However now Im getting "invalid syntax" error on this line:

    self.connect(self.actionAddLayer, SIGNAL("activated()"), self.addLayer)

    I fail to see why I'm getting invalid syntax; basically since I'm using a code from a tutorial. Do I need to install some dependencies or the like perhaps? I'm in the dark.

    cheers

    Code:
    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()
            # Move the app window to upper left
            wnd.move(100,100)
            wnd.show()
    
            # run!
            retval = app.exec_()
    
            # exit
            QgsApplication.exitQgis()
            sys.exit(retval)
    
        if __name__ == "__main__":
        main(sys.argv)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    There is no way to tell without the complete error message. For starters check the previous line for mismatched parens, mismatched quotes, etc.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    You appear to have an unterminated quotation in the previous statement.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    If you can, post a link to this fine tutorial. You've posted
    Code:
            self.actionZoomIn = QAction(QIcon("(qgis_prefix + \ "/share/qgis/themes/classic/mActionZoomIn.png"), \
            "Zoom In", self.frame)
    with 5 quote marks and a misplaced backslash.

    It's an unparsable mess like this.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    Apparently as a new user here I'm not allowed to post links. However google 'qgis-1.6.0_coding-compilation_guide_en' and it comes up as first as a pdf. Check out section "4 Creating PyQGIS Applications". The code and explanation is listed there.

    I'm new to python so if the code is bad it might naturally be me making a mess of it though.

    What I found so far is that the syntax errors I got might be due to an import error, since I tried to import qgis.core * in the shell and then I got the message: ImportError: No module named qgis.core

    There are fixes for this both in the guide, as well as on a whole lot of others places online where it says to change the pythonpath to the python libs under the QGIS installation. I did that but still got the same problem.

    While I'm in the QGIS program I'm using the internal python console and there I can import the package. Nevertheless when I'm trying to import it from any other IDE such as IDLE, eclipse, wing etc. it still gives me this import error.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    Wow, nasty. The link to the original LaTeX source of the pdf is broken. It would be easier to extract the program from the .tex file. I used pdftotext followed by editing. The pdf has errors and says
    In line 20 we store this value from the environment for later use.
    Line 20 is missing. There is a line that grabs (probably the correct) value from the environment. Maybe the program is alright. This version of the program doesn't have python2 syntax errors. So try it.
    Code:
    # Loosely based on:
    #     Original C++ Tutorial 2 by Tim Sutton
    #     ported to Python by Martin Dobias
    #     with enhancements by Gary Sherman for FOSS4G2007
    # Licensed under the terms of GNU GPL 2
    
    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("QGISHOME")
    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
    
        def zoomIn(self): # Set the map tool to zoom in
            self.canvas.setMapTool(self.toolZoomIn)
    
        def zoomOut(self): # Set the map tool to zoom out
            self.canvas.setMapTool(self.toolZoomOut)
    
        def pan(self): # Set the map tool to
            self.canvas.setMapTool(self.toolPan)
    
        def zoomFull(self): # Zoom to full extent of layer
            self.canvas.zoomFullExtent()
    
        def addLayer(self): # Add an OGR layer to the map
            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()
        # Move the app window to upper left
        wnd.move(100,100)
        wnd.show()
        # run!
        retval = app.exec_()
        # exit
        QgsApplication.exitQgis()
        sys.exit(retval)
    
    if __name__ == "__main__":
        main(sys.argv)
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    8
    Rep Power
    0
    that version worked better. Thank you.

    cheers

    Originally Posted by b49P23TIvg
    Wow, nasty. The link to the original LaTeX source of the pdf is broken. It would be easier to extract the program from the .tex file. I used pdftotext followed by editing. The pdf has errors and says
    Line 20 is missing. There is a line that grabs (probably the correct) value from the environment. Maybe the program is alright. This version of the program doesn't have python2 syntax errors. So try it.
    Code:
    # Loosely based on:
    #     Original C++ Tutorial 2 by Tim Sutton
    #     ported to Python by Martin Dobias
    #     with enhancements by Gary Sherman for FOSS4G2007
    # Licensed under the terms of GNU GPL 2
    
    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("QGISHOME")
    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
    
        def zoomIn(self): # Set the map tool to zoom in
            self.canvas.setMapTool(self.toolZoomIn)
    
        def zoomOut(self): # Set the map tool to zoom out
            self.canvas.setMapTool(self.toolZoomOut)
    
        def pan(self): # Set the map tool to
            self.canvas.setMapTool(self.toolPan)
    
        def zoomFull(self): # Zoom to full extent of layer
            self.canvas.zoomFullExtent()
    
        def addLayer(self): # Add an OGR layer to the map
            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()
        # Move the app window to upper left
        wnd.move(100,100)
        wnd.show()
        # run!
        retval = app.exec_()
        # exit
        QgsApplication.exitQgis()
        sys.exit(retval)
    
    if __name__ == "__main__":
        main(sys.argv)

IMN logo majestic logo threadwatch logo seochat tools logo