Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread and loading QGIS memory layers
Forum Updated to NodeBB v4.3 + New Features

QThread and loading QGIS memory layers

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matthijs.Bon
    wrote on last edited by Matthijs.Bon
    #1

    Hi, I know this is a QT forum and my question seems to be mostly about QGIS plugins, but I think, after many debugs, that the problem is somewhere within QThread, therefore, I post here as well, and the GIS stackexchange so far yielded nothing... I've been working on a small plugin recently and I wanted to add a progress bar to it, since there are tasks in this plugin that take some time to complete. I looked at this and this, but so far I've encountered a problem which I can't seem to solve.

    Whenever I run the plugin without moving it to another thread, it runs perfectly fine, everything works (but the GUI is unresponsive for a short while). But when I move the worker (the python class that does the heavy lifting) to another thread, suddenly, I can't seem to add the QgsVectorLayer (which is a memory layer) to the iface.LegendInterface anymore. Everything else still works fine, but the layer just does not show up in the legend in QGIS. So far, this is my code, I've left out the specific methods and imported code, since that all works if I don't move it to a thread (so I figure that can't be the problem):

    from PyQt4 import QtCore
    from qgis.core import *
    from qgis.gui import *
    import utility_functions as uf
    
    import traceback
    
    class Worker(QtCore.QObject):
    
    def __init__(self, iface, data, layer, plugindir, outfile):
        QtCore.QObject.__init__(self)
        self.iface = iface
        self.data = data
        self.layer = layer
        self.plugindir = plugindir
        self.outfile = outfile
        self.killed = False
        self.PathLayer = None
    
    finished = QtCore.pyqtSignal(object)
    error = QtCore.pyqtSignal(Exception, basestring)
    progress = QtCore.pyqtSignal(float)
    layerCreated = QtCore.pyqtSignal(object) # tried for debugging, does not work
    
    def run(self):
        ret = None
        try:
            # Create edgelist
            edgelist = self.selectEdges()
    
            # Select features on layer and copy to new layer
            features = self.selectFeatures(edgelist, self.layer)
    

    Everything works perfectly fine up until here. In the method 'createNewLayer', the memory layer should be (1) created using QgsVectorLayer, and (2) Loaded using QgsMapLayerRegistry.instance().addMapLayer(layer)

            self.createNewLayer(features)
    
            # Apply layer style
            if not self.PathLayer:
                lyr = uf.getLegendLayerByName(self.iface, 'Path')
            else:
                lyr = self.PathLayer
            self.applyStyle(lyr)
    
            if self.killed is False:
                self.progress.emit(len(self.data))
                ret = lyr
        except Exception, e:
            # forward the exception upstream
            self.error.emit(e, traceback.format_exc())
        self.finished.emit(ret)
    
    

    But somehow, the moving to another thread messes with the layer and it can't be added. What's even weirder: I can see the layer when I type into the QGIS python console: QgsMapLayerRegistry.instance().mapLayers() and the return variable ret is a pointer to a vectorlayer. So I'm guessing that the way QThreads work, messes up the QgsMapLayerRegistry OR the iface.LegendInterface.

    I'm working on macOS Sierra, QGIS 2.14, Qt 4.8.6, but I've also tried the plugin on a W10 machine, QGIS 2.18, same problems. Any help would be greatly appreciated, this problem is messing with my brain!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Sorry not an answer, but I'd recommend asking this to the QGIS folks, they might be more informed about the interaction of their classes with QThread.

      One of the thing to keep in mind is that GUI related classes should not be manipulated outside the GUI thread which is usually the application main thread e.g. you can't manipulate a QPixmap in a thread but you can a QImage.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved