I'm pretty new to python and pyqgis so the question will be pretty trivial..

I'm writing a plugin and in one script I have a class and some internal functions..

class CreateModelDialog(QDialog, FORM_CLASS):
    def __init__(self, iface):
    def openSr(self):
        projSelector = QgsGenericProjectionSelector()

Then I have to use the last line (that is the EPSG cose selected and displayed in srText QLineEdit of the QT dialog in another function of the same script:

def createModel(self):
        modelName = self.bxDBname.text()
        modelType = self.cmbBxModelType.currentText()
        crs = self.srText.setText(projSelector.selectedAuthId())

createModel(modelName, modelType, crs)

But I get an error:

NameError: global name 'projSelector' is not defined

I'm sure that is due to the fact that projSelector is a variable that exists only in the local open Sr function.

So the question is: how should I modify the function in order to make the variable callable?


You can create instance vairiables with self keyword:

def openSr(self):
        self.projSelector = QgsGenericProjectionSelector()

Now your self.projSelector variable is ready to use in other methods.

dialog = CreateModelDialog(iface)

Will return your variable with this object.

  • Tried with your command but it doesn't work.. I solved creating projSelector as a global variable: global projSelector – matteo Nov 24 '15 at 9:49

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.