How is it possible to render TIFF image from the path in the custom plugin? I tried to do like this:

canvas = QgsMapCanvas()
layer = QgsRasterLayer('my.tif',
QgsProject.instance().addMapLayer(layer, False)

also tried iface.addRasterLayer() and gsMapLayerRegistry.instance().addMapLayer(layer) - these two ways are definitely workable from the qgis python console however in the plugin I have warnings like

    Warning: QObject::setParent: Cannot set parent, new parent is in a different thread
Warning: QObject::connect: Cannot queue arguments of type 'QList<QgsMapLayer*>'
(Make sure 'QList<QgsMapLayer*>' is registered using qRegisterMetaType().)

and nothing is rendered.

I'm trying to do it from the separate class in the plugin package if it is a necessary information.

1 Answer 1


The __init__() method in your main plugin class should create a reference to the qgisinterface object as a class attribute e.g. self.iface. You should access this self.iface object and load your raster layer like this:

self.iface.addRasterLayer(path, 'My Raster')

I have added a minimal working plugin structure based on the example by Martin Dobias here.

I'm not sure from your question if you want to load your raster using a relative path to a tif file in the plugin directory or just an absolute path to a locally saved file. I have included examples of both.

to test this example, create a new directory in your qgis plugins folder. Save the script below as __init__.py and place it in this directory.

import os

from PyQt5.QtWidgets import QAction

def classFactory(iface):
    return MinimalPlugin(iface)

class MinimalPlugin:
    def __init__(self, iface):
        self.iface = iface

    def initGui(self):
        self.action = QAction('Load Raster', self.iface.mainWindow())

    def unload(self):
        del self.action

    def run(self):
        #Using relative path to load a tif file located in the
        #plugin directory
        folder = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(folder, 'my.tif')
        #Or uncomment and change the path below to use an
        #absolute path to a tif file on your local system
        #path = 'C:\\path\\to\\my.tif'
        self.iface.addRasterLayer(path, 'My Raster')

Also save the following as a text file called metadata in the same directory:

description=Minimal plugin
about=About this minimal plugin

If you want to load the raster with a relative path from the plugin directory, save your 'my.tif' file there too.

You can then enable and test the minimal plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.