2016-09-26 59 views
0

我是QT,Python和QGIS的新手。我安装了“Plugin Builder”插件并生成了一个Dockwidget。我可以使用qtcreator更改小部件,并且正在学习如何实现信号和插槽以使用我自己的插件。将QGIS插件逻辑拉成独立应用程序

现在,我的问题。有没有简单的方法,我可以删除QGIS iface,并在QGIS之外使用我的插件逻辑?我目前实际上并没有使用任何PyQGIS库,但我想保留由“Plugin Builder”生成的QT接口和Python代码/结构。有没有办法做到这一点?

谢谢。

回答

0

是的,有办法做到这一点。但到目前为止,我发现我们需要将整个Qgis库复制到最终的软件包中。设置正确的路径里面的代码QGIS应用是非常重要的如下:

QgsApplication.setPrefixPath(r"C:\OSGeo4W\apps\qgis", True) 
QgsApplication.initQgis() 
QgsProject.instance().setFileName(strProjectName) 

此外,我们需要写文件,并在年底

QgsProject.instance().write() 
QgsApplication.exitQgis() 

关闭这里的立场快照我创建的独立软件包。代码需要修改一些变量才能工作。

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from qgis.core import * 
from qgis.gui import * 
import os, datetime 

class CreateQgs(): 

    def initQgsFile(self, outputFolder, stopRadius): 

     strProjectName = str(outputFolder) + "\\" + "PhotoLocationMap.qgs" 
     QgsApplication.setPrefixPath(r"C:\OSGeo4W\apps\qgis", True) 
     QgsApplication.initQgis() 
     QgsProject.instance().setFileName(strProjectName) 

     highwayShapeFilePath = "C:/Shapefiles/Highway.shp" 
     arterialShapeFilePath = "C:/Shapefiles/StreetsMajor.shp" 

     highwayLayer = QgsVectorLayer(self.highwayShapeFilePath, 'HighwayDB' , 'ogr') 
     arterialLayer = QgsVectorLayer(self.arterialShapeFilePath, 'ArterialDB', 'ogr') 

     symbols = highwayLayer.rendererV2().symbols() 
     sym = symbols[0] 
     sym.setColor(QColor.fromRgb(255,94,94)) 
     highwayLayer.triggerRepaint() 

     symbols = arterialLayer.rendererV2().symbols() 
     sym = symbols[0] 
     sym.setColor(QColor.fromRgb(76,138,245)) 
     arterialLayer.triggerRepaint() 

     mapInstance = QgsMapLayerRegistry.instance() 

     mapInstance.instance().addMapLayer(arterialLayer) 
     mapInstance.instance().addMapLayer(highwayLayer) 

     QgsProject.instance().write() 
     QgsApplication.exitQgis() 

def unitTest(): 
    app = QgsApplication(sys.argv, True) 
    photoFolderPath = 'C:\Test\QGis\TestPics' 
    CreateQgsFile = CreateQgs() 
    CreateQgsFile.initQgsFile(photoFolderPath, 128) 

if __name__ == "__main__": 
    unitTest()