Bug report #9015
API: Using a QgsVectorLayer from an unusable uri crashes QGIS
|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|
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")
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.
#1 Updated by Luigi Pirelli about 7 years ago
try to use isValid() call before to use the layer.
#2 Updated by Bernhard Ströbl about 7 years ago
- Resolution set to invalid
- Status changed from Open to Closed
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.