[QGIS-Developer] Any gotchas related to working with QgsVectorLayer inside a QgsTask?

Tejas L tejvan at live.com
Tue Mar 5 08:56:09 PST 2019


Hello Team,

I know that the UI must not be updated from the QgsTask.run() method. Are there any similar restrictions on loading QgsVectorLayer from a file or creating new layers from QgsTask.run() ?

I find that some operations on a GPKG layer do not work as expected when run from a QgsTask.
These operations work fine when they are done from a foreground thread.

Example:

class MyTask(QgsTask)
...
def run(self):
   self.layer = QgsVectorLayer("geom.gpkg", "test_geom", "ogr")

def finished(self, res):
   if self.layer.isValid():
       QgsProject.instance().addMapLayer(self.layer)    # Layer loads, but attribute table is not editable

--------------------------
On the other hand, the following works just as expected. The attribute table is editable.

layer = QgsVectorLayer("geom.gpkg", "test_geom", "ogr")
QgsProject.instance().addMapLayer(layer)   # Layer loads and attribute table is editable.

What are the best practices for working with a QgsVectorLayer from within a QgsTask?

Regards,
Tej




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190305/ec3d9dca/attachment.html>


More information about the QGIS-Developer mailing list