我尝试开发一个插件,以从插件中向qgis添加postgis层,作为学习python的一部分。
如果我从qgis中的python控制台运行它,则用于添加postgis图层的代码可以从食谱中获取,并且可以正常工作(如果在对话框result == 1中按了OK按钮,则应该将我的图层添加到qgis中)。
但是,如果我从插件运行它,则会引发消息NameError:未定义名称'QgsDataSourceURI'。为什么从插件运行该错误?
我如何从插件/函数内部添加图层与从Python控制台添加图层有区别吗?
def run(self):
"""Run method that performs all the real work"""
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result == 1:
# Do something useful here - delete the line containing pass and
# substitute with your code.
uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "test", "postgres", "postgres")
#set database schema, table name, geometry column and optionaly subset(WHERE clause)
uri.setDataSource ("basic", "cities", "geom")
# Defining the layer name and layer type for QGIS?
vlayer=QgsVectorLayer (uri .uri() ,"cities","postgres")
我正在尝试使用您的模式在python控制台QGIS中添加PostGIS图层,但未成功-“ NameError:未定义名称'self'”。这是我的代码gis.stackexchange.com/questions/245985/…–
—
简