2016-09-26 61 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()