3

Using PyQGIS, I am developing a QGIS plugin that includes a filter function. It should make handling easier for users (internal) who rarely use QGIS.

Now I have created a interface with the QT Designer that contains several checkboxes.

  • Each checkbox contains criteria that can be used to filter. Every checkbox is set at the start.
  • With uncheck the checkbox, a more precise selection is activated via a combo box. Let's assume every combo box has 3 different attributes.
  • At the end, the filter should be started with the appropriate selection via the push button. The visualization looks like this:

enter image description here

The code gets pretty fast, pretty long, and cluttered.

def Filter_starten():
    if checkbox1.isCheck():
        "take attributes"
    else:
        "take other attributes"
    
    if checkbox2.isCheck():
        "take attributes"
    else:
        "take other attributes"
    
    if checkbox1.isCheck() and checkbox2.isCheck():
        "take attributes"
    else:
        "take other attributes"
    
    if checkbox2.isCheck() and checkbox3.isCheck():
        "take attributes"
    else:
        "take other attributes"

And so on..

How can I efficiently program the multitude of possible options in the Filter_starten() function? How do I avoid a confusing number of if-clauses?

1
  • 3
    I'm not sure if this question is really a GIS question or could be instead asked in StackOverflow. However, you might be interested in reviewing the strategy pattern, which I've used in the Load Them All plugin to apply a number of filters (and all their possible combinations) on both vector and raster layers. Have a look at this diagram and at the code itself. Commented Aug 31, 2021 at 14:28

1 Answer 1

4

Perhaps you could loop through the checkboxes, using the isChecked() method to ascertain whether they are checked and if so, set all combobox items to checked. Then eventually, get all the checked items from the comboboxes and store them in a dictionary.

Something like:

## make a dict using the comboboxes as keys and their various options as values
comboboxes = {combobox1:['option1', 'option2', 'option3'], 
              combobox2:['option1', 'option2', 'option3'], 
              combobox3:['option1', 'option2', 'option3']}

## initialise an empty dict
checked_items = {}

## loop through checkboxes
for i,cb in enumerate([checkbox1, checkbox2, checkbox3]):
  if cb.isChecked():
    ## if checkbox is checked, set all items in combobox to checked
    list(comboboxes.keys())[i].setCheckedItems(comboboxes[list(comboboxes.keys())[i]])

  ## set the checked items of each combobox against the combobox in the checked items dict
  checked_items[list(comboboxes.keys())[i]] = list(comboboxes.keys())[i].checkedItems()
    
## do something with the checked items

This way, however the comboboxes are configured will be captured when the button is clicked, and automatically checked based on the checkboxes.

I can't test this right now, but hopefully it gives you some ideas.

I am assuming you are using QgsCheckableComboBox for the selections should the checkboxes be unchecked.

Your Answer

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

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