1. 程式人生 > >QGIS3和QGIS2外掛開發差異記錄

QGIS3和QGIS2外掛開發差異記錄

qgis2外掛開發的入門級教程
http://www.qgistutorials.com/zh_TW/docs/building_a_python_plugin.html

仿造該教程在qgis3當中進行開發,會碰到許多坑。這裡簡單記錄一下。

1.
QGIS3 中,QFileDialog 位於PyQt5.QtWidgets下,所以引用方式為:
from PyQt5.QtWidgets import QAction, QfileDialog

QGIS2中, QFileDialog 位於PyQt5.QtGui下
引用方式為:
from PyQt5.QtGui import QfileDialog
2.


QGIS3中,遍歷圖層的方法變為:
layers = [layer for layer in QgsProject.instance().mapLayers().values()]
同時需要加上引用:
from qgis.core import QgsProject

在QGIS2中,寫法則是:
layers = self.iface.legendInterface().layers()
3.
qgis3中開啟文件,使用wb.不然會報錯。
output_file = open(filename, ‘wb’)

參考:
https://gis.stackexchange.com/questions/269012/attributeerror-qgsvectorlayer-object-has-no-attribute-pendingfields