Вот мой код
-----------------------------------------------------
shapeviewer.py
-----------------------------------------------------
Код: Выделить всё
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import sys
import os
# Import our GUI
from shapeviewer_gui import Ui_MainWindow
# Environment variable QGISHOME must be set to the install directory
# before running the application
#qgis_prefix = os.getenv("QGISHOME")
class ShapeViewer(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("ShapeViewer")
# Create the map canvas
self.canvas = QgsMapCanvas()
self.canvas.useImageToRender(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)
# layout is set - open a layer
# 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()
#ymbol = symbols[0]
#ymbol.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 = ShapeViewer()
# 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)
shapeviewer_gui.py
---------------------------------------------------------
Код: Выделить всё
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'shapeviewer_gui.ui'
#
# Created: Sat Feb 7 15:52:11 2009
# by: PyQt4 UI code generator 4.4.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(525, 335)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridlayout = QtGui.QGridLayout(self.centralwidget)
self.gridlayout.setObjectName("gridlayout")
self.frame = QtGui.QFrame(self.centralwidget)
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridlayout.addWidget(self.frame, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 525, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
shapeviewer.ui
------------------------------------------------
Код: Выделить всё
<ui version="4.0" >
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>525</width>
<height>335</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget" >
<layout class="QGridLayout" >
<item row="0" column="0" >
<widget class="QFrame" name="frame" >
<property name="frameShape" >
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow" >
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>525</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar" />
</widget>
<resources/>
<connections/>
</ui>