1

I use this code as a rubber from my QGIS canvas:

def obrisi_sve(iface):
    rubber = [i for i in iface.mapCanvas().scene().items() if issubclass(type(i), qgis.utils.gui.QgsRubberBand)]
    for ver in rubber:
        if ver in iface.mapCanvas().scene().items():
            iface.mapCanvas().scene().removeItem(ver)

When I run it from Python console inside QGIS it works ok, but when I use it in plugin I get error: global name 'qgis' is not defined. To call this file from plugin I use these lines of code:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from math import *
import os

import brisanje_rubber

from PyQt4 import QtGui, uic
from PyQt4.QtCore import QObject
from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import QVariant
from qgis.gui import QgsMessageBar
from qgis.gui import QgsMapToolEmitPoint
from qgis.utils import iface
from qgis import *


def __init__(self, parent=None):
    self.setupUi(self)
    self.pushButton_6.clicked.connect(self.obrisi_IPP)
def obrisi_IPP(self):
    brisanje_rubber.obrisi_sve(iface)

How can I solve this?

0

In a plugin you need to import the modules. On the other hand, this is not correct qgis.utils.gui.QgsRubberBand

Sample

from qgis.gui import QgsRubberBand
from qgis.utils import iface

def obrisi_sve(iface):
    rubber = [i for i in iface.mapCanvas().scene().items() if issubclass(type(i), QgsRubberBand)]
    for ver in rubber:
        if ver in iface.mapCanvas().scene().items():
            iface.mapCanvas().scene().removeItem(ver)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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