1

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):
        QDialog.__init__(self)
....
    def openSr(self):
        projSelector = QgsGenericProjectionSelector()
        projSelector.exec_()
        self.srText.setText(projSelector.selectedAuthId())

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?

4

You can create instance vairiables with self keyword:

def openSr(self):
        self.projSelector = QgsGenericProjectionSelector()
        self.projSelector.exec_()
        self.srText.setText(self.projSelector.selectedAuthId())

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

dialog = CreateModelDialog(iface)
...
dialog.projSelector

Will return your variable with this object.

1
  • 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.