Bug report #9015
API: Using a QgsVectorLayer from an unusable uri crashes QGIS
Status: | Closed | ||
---|---|---|---|
Priority: | Normal | ||
Assignee: | - | ||
Category: | Data Provider | ||
Affected QGIS version: | 2.0.1 | Regression?: | No |
Operating System: | Easy fix?: | No | |
Pull Request or Patch supplied: | No | Resolution: | invalid |
Crashes QGIS or corrupts data: | Yes | Copied to github as #: | 17670 |
Description
In my Python plugin I create layers from PostGIS tables# db is a valid QtSql.QSqlDataBase object
uri = QgsDataSourceURI()
uri.setConnection(db.hostName(), "5432", db.databaseName(), db.userName(), db.password())
uri.setDataSource(schemaName, tableName, geomColumnName)
vlayer = QgsVectorLayer(uri.uri(), displayName, "postgres")
QgsMapLayerRegistry.instance().addMapLayers([vlayer])
if I try to use this vlayer further in my script QGIS simply crashes. The cause seems to be that the user has no SELECT rights on the relation in question. IMHO the constructor of QgsVectorLayer should throw an error if the uri is somehow not valid.
History
#1 Updated by Luigi Pirelli almost 11 years ago
try to use isValid() call before to use the layer.
http://www.qgis.org/api/classQgsDataProvider.html#a1f805f593a0fa0be58102e278114858b
#2 Updated by Bernhard Ströbl almost 11 years ago
- Resolution set to invalid
- Status changed from Open to Closed
Hi Luigi,
and thanks for the tip. I was studying the api doc for QgsVectorLayer up and down. I quickly checked and realized that you can use almost anything (e.g. none existing shp file) to instatiate a QgsVectorLayer but it's data provider is not valid then either.