3

This is my first time in trying to develop any automation within the QGIS environment.

I am trying to change a user defined project variable – DMA select – programmatically.

enter image description here

The changed project variable is then used to change the parameter in rule based symbologies for 2 layers using the single variable.

enter image description here

enter image description here

I have written a script that can change the project variable on the Python console when I change the variable manually in the script.

project = QgsProject.instance()
QgsExpressionContextUtils.setProjectVariable(project,'DMA_Select','NORTHMEAD')
iface.mapCanvas().refreshAllLayers()

I have also created a form in QT Designer with a dropdown of the possible values for the project variable.

enter image description here

What do I do next to combine this together so that the selected vale from the combo box can change the project variable?

I've searched around the internet and guides/books for this particular solution and have not came up with anything. I can see that we can bind the form/UI to layers but cannot find an example where it is used to change a project or global variable.


I have inserted in the file change_variable.py - between def initGui(self): and def unload(self):

    def VariableValue(self):
        self.setVariableValue(value)):
        project = QgsProject.instance()
        QgsExpressionContextUtils.setProjectVariable(project,'DMA_Select', value)

        #iface is accessed by self.iface inside the plugin
        self.iface.mapCanvas().refreshAllLayers()

and then in the def run(self): section after

#comboBox is the UI objectName
self.dlg.comboBox.currentTextChanged.connect(lambda currentText: setVariableValue(currentText))

When I open QGIS i get the error message enter image description here on the plugin load enter image description here


change_variable.py changed enter image description here

The plugin loads okay but when run and dropdown is selected I get this error enter image description here

2
  • Does this form you showed in the last screenshot belong to a qgis plugin or is it just an independent window? Oct 2, 2020 at 10:43
  • I corrected the code in my answer. Check now Oct 4, 2020 at 16:41

1 Answer 1

2

I would solve this issue by creating my own QGIS plugin using PluginBuilder. It is not complicated and there are many guides on how to do that. You need it to be a plugin so that the main QGIS app components can be accessible.

After creating the plugin find the .ui file in the main plugin directory and add the UI components the way you created them before in Qt Designer.

Now, in the main plugin code (PluginName.py):

Define a setVariableValue function somewhere in the Plugin class - it is necessary for the function to have access to the main QGIS application components.

def setVariableValue(self, value):
    project = QgsProject.instance()
    QgsExpressionContextUtils.setProjectVariable(project,'DMA_Select', value)
    #iface is accessed by self.iface inside the plugin
    self.iface.mapCanvas().refreshAllLayers()

Finally, connect the function to the comboBox currentTextChanged slot in the run(self) method in the main plugin class. The variable value will be changed every time you select a new value from the list.

#comboBox is the UI objectName
self.dlg.comboBox.currentTextChanged.connect(lambda currentText: self.setVariableValue(currentText))

Then you need to reload the plugin.

10
  • Many thanks Leon. I have succeeded up to UI component which opens in QGIS. Once I added the code you gave the plugin has errors. I am not sure how to attach the change_variable.py to this comment so that you can see Oct 4, 2020 at 7:58
  • Maybe edit the main post and add the screenshot there. Also write what type of error you are getting. Btw, after changing the plugin code always deploy it first and then reload it in QGIS (by restarting QGIS or using Plugin Reloader plugin). Oct 4, 2020 at 12:11
  • I have put the text of me edits and screenshot of the error in the main post Oct 4, 2020 at 14:54
  • My bad, I pasted the code with a syntax error. Check it now with the corrected version of my code. Oct 4, 2020 at 16:38
  • I used the code...plugin loads okay now but error when dropdown box selected...have added to main post Oct 4, 2020 at 17:53

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.