3

I am working on a QGIS plugin. The plugin has a main class running all the operations. I was trying to add a progress window in it. The window should load when a button is clicked running a method called "process" of same class at the back. I realized that for this I might need to use multi-threading.

Now I made changes by sending the process method to another thread. Everything works fine, except that the layer I was rendering from the the class of process method just stops working. It feels like everything has lost its connectivity. Even when I turn the layer off map canvas still keeps showing it in QGIS.

I also get this warning:

Warning: QObject::setParent: Cannot set parent, new parent is in a different thread

Can anybody tell me what the issue could be?

Vince
  • 20,017
  • 15
  • 45
  • 64
  • 1
    You will need to add your code and some more details or your question will likely be closed. You don't say if you are using QgsTask or QThread. My guess is you are trying to load a layer from the background thread. Please see the bold warning here: https://docs.qgis.org/3.22/en/docs/pyqgis_developer_cookbook/tasks.html#introduction about unsafe thread operations from QgsTask. The same goes for QThread. If your thread class produces a layer, you can use pyqtsignal to emit the layer object and load it from the main thread. – Ben W May 05 '22 at 05:23
  • 1
    You may find these recent related questions and answers useful: https://gis.stackexchange.com/questions/429688/setting-up-thread-with-busy-progress-indicator-or-gif-in-pyqgis, https://gis.stackexchange.com/questions/430230/sharing-class-variables-across-thread-qgstasks-with-pyqgis and https://gis.stackexchange.com/questions/430422/conditional-signals-in-qgstask-managing-algorithm-output – Ben W May 05 '22 at 05:24
  • Thank you for your reply. I cannot share the code of the plugin. I am not allowed to do it. I just wanted to get an idea how this is possible theoretically. – Haris Harris May 05 '22 at 05:26
  • 1
    This question/ answer here is also relevant (about QThread). It should be helpful for you: https://gis.stackexchange.com/questions/420003/qthread-map-not-loading-after-the-thread-is-done/420126#420126 – Ben W May 05 '22 at 05:29
  • Thanks alot for the resources. – Haris Harris May 05 '22 at 05:30
  • You're welcome, the general rule is that all communication and transfer of objects between threads should be via signals. I'm sure you can find more examples if you search. Good luck. – Ben W May 05 '22 at 05:40

0 Answers0