'QgsLayerTreeLayer' is not defined when running from python plugin but not when run in the console in QGIS...

Adding a new switch to a C9300 stack

Why does conservation of energy give a wrong result?

Can we use the stored gravitational potential energy of a building to produce power?

Isn't using the Extrusion Multiplier like cheating?

How can animals be objects of ethics without being subjects as well?

Does Improved Divine Strike trigger when a paladin makes an unarmed strike?

Explain the objections to these measures against human trafficking

What is the purpose of easy combat scenarios that don't need resource expenditure?

What is the in-universe cost of a TIE fighter?

What creature do these Alchemical Humonculus actions target?

How do I say "Brexit" in Latin?

Parsing a string of key-value pairs as a dictionary

Using only 1s, make 29 with the minimum number of digits

Why does String.replaceAll() work differently in Java 8 from Java 9?

A minimum of two personnel "are" or "is"?

Why is working on the same position for more than 15 years not a red flag?

Does Windows 10's telemetry include sending *.doc files if Word crashed?

If I delete my router's history can my ISP still provide it to my parents?

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Process to change collation on a database

Would these multi-classing house rules cause unintended problems?

Are there neural networks with very few nodes that decently solve non-trivial problems?

Every character has a name - does this lead to too many named characters?

Avoiding morning and evening handshakes



'QgsLayerTreeLayer' is not defined when running from python plugin but not when run in the console in QGIS 3


Is there a way to run the spatial query plugin from QGIS python console?How to call the interpolation plugin from the python console?Running QGIS viewshed plugin from console?heat map plugin from QGIS python consoleCrash QGis 2.16 by defining a memory vector layer with pythonCan not run grass:r.to.vect in Qgis consolePlugin crashes QGIS 2.18 if running simuntaneously with python consoleCalling `Rscript` from QGIS Python Console / QGIS Plugin not workingGrouping layers using PyQGIS makes them not visible on canvas?Qgis Python console not working













0















When I run the following in a plugin I get




name 'QgsLayerTreeLayer' is not defined




print('Loading raster layer...')
#QgsProject.instance().removeMapLayer(tLayer)
print("%s called %s" % (filePath, fname))
tLayer = QgsRasterLayer(filePath, fname, 'gdal')
QgsProject.instance().addMapLayer(tLayer, False)
print('layerTreeCanvasBridge')
layerTree = iface.layerTreeCanvasBridge().rootGroup()
print(layerTree)
layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


However in python console it works



filePath=r'c:resourceslandcover.img'
tLayer = QgsRasterLayer(filePath, 'imagery', 'gdal')
layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


The plugin imports



from qgis.core import QgsApplication, QgsVectorLayer, QgsRasterLayer, QgsProject 
from qgis.utils import iface


Should I import something else? This is based on https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html









share



























    0















    When I run the following in a plugin I get




    name 'QgsLayerTreeLayer' is not defined




    print('Loading raster layer...')
    #QgsProject.instance().removeMapLayer(tLayer)
    print("%s called %s" % (filePath, fname))
    tLayer = QgsRasterLayer(filePath, fname, 'gdal')
    QgsProject.instance().addMapLayer(tLayer, False)
    print('layerTreeCanvasBridge')
    layerTree = iface.layerTreeCanvasBridge().rootGroup()
    print(layerTree)
    layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


    However in python console it works



    filePath=r'c:resourceslandcover.img'
    tLayer = QgsRasterLayer(filePath, 'imagery', 'gdal')
    layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


    The plugin imports



    from qgis.core import QgsApplication, QgsVectorLayer, QgsRasterLayer, QgsProject 
    from qgis.utils import iface


    Should I import something else? This is based on https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html









    share

























      0












      0








      0








      When I run the following in a plugin I get




      name 'QgsLayerTreeLayer' is not defined




      print('Loading raster layer...')
      #QgsProject.instance().removeMapLayer(tLayer)
      print("%s called %s" % (filePath, fname))
      tLayer = QgsRasterLayer(filePath, fname, 'gdal')
      QgsProject.instance().addMapLayer(tLayer, False)
      print('layerTreeCanvasBridge')
      layerTree = iface.layerTreeCanvasBridge().rootGroup()
      print(layerTree)
      layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


      However in python console it works



      filePath=r'c:resourceslandcover.img'
      tLayer = QgsRasterLayer(filePath, 'imagery', 'gdal')
      layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


      The plugin imports



      from qgis.core import QgsApplication, QgsVectorLayer, QgsRasterLayer, QgsProject 
      from qgis.utils import iface


      Should I import something else? This is based on https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html









      share














      When I run the following in a plugin I get




      name 'QgsLayerTreeLayer' is not defined




      print('Loading raster layer...')
      #QgsProject.instance().removeMapLayer(tLayer)
      print("%s called %s" % (filePath, fname))
      tLayer = QgsRasterLayer(filePath, fname, 'gdal')
      QgsProject.instance().addMapLayer(tLayer, False)
      print('layerTreeCanvasBridge')
      layerTree = iface.layerTreeCanvasBridge().rootGroup()
      print(layerTree)
      layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


      However in python console it works



      filePath=r'c:resourceslandcover.img'
      tLayer = QgsRasterLayer(filePath, 'imagery', 'gdal')
      layerTree.insertChildNode(-1, QgsLayerTreeLayer(tLayer))


      The plugin imports



      from qgis.core import QgsApplication, QgsVectorLayer, QgsRasterLayer, QgsProject 
      from qgis.utils import iface


      Should I import something else? This is based on https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html







      qgis pyqgis





      share












      share










      share



      share










      asked 1 min ago









      GeorgeCGeorgeC

      2,86432980




      2,86432980






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f314114%2fqgslayertreelayer-is-not-defined-when-running-from-python-plugin-but-not-when%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f314114%2fqgslayertreelayer-is-not-defined-when-running-from-python-plugin-but-not-when%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

          Щит и меч (фильм) Содержание Названия серий | Сюжет |...

          Энтрерриос (город) Содержание История | Географическое...