2

I am developing a plugin for the GIS software, QGIS. I created a QTableWidget and wish to extract values from it:

Image

Problem is, I use a lot of for loops and if else statements which, up until the last few lines, seems to work fine. I can't seem to follow the logic now as the line print constraint_name only prints off the last value "Example_2". I could take it out of its corresponding else statement and then it will print all values correctly but I need to set it inside a condition:

qTable = self.dockwidget.tableWidget  # QTableWidget
example_group = root.findGroup('Main group')  # Group containing sub groups
all_items = []
gis_map = QgsMapLayerRegistry.instance().mapLayersByName( "Map" )[0]  # Layer map in QGIS
idx = gis_map.fieldNameIndex("Rank")  # Get "Rank" attribute field from gis_map
for row in range(qTable.rowCount()):
    for col in [0]:  # For first column "Constraint name"
        constraint_item = qTable.item(row, col)                            
        constraint_name = str(constraint_item.text())
    for col in [1]:  # For second column "Rank"
        item = qTable.item(row, col)
        item_string = str(item.text())
        all_items.append(item_string)
        for group in example_group.children():  # Search for specific group
            if group.name() == "Sub group":
                if len(set(all_items)) == 1:  # If all items are the same
                    # If "Rank" field exists in layer map
                    if idx == -1:
                        print 'success'
                    else: 
                        print 'fail'                        
                else:
                    if idx == -1:
                        print constraint_name
                    else:
                        print 'fail'   

Is there a way to tidy this up and still get the correct results?

8
  • 4
    for col in [0]:?? Something seems fishy there
    – R Nar
    Commented May 26, 2016 at 13:30
  • 2
    both of your 2nd level for loops are useless. for col in [0] and for col in [1] can be refactored to nothing, since they each execute exactly once with a known value. Commented May 26, 2016 at 13:35
  • 2
    Also it seems that your innermost if statements can be reversed, since you print fail if idx == -1 regardless of the value of len(set(all_items)) Commented May 26, 2016 at 13:36
  • @RNar - I'm not sure but isn't that the first column of the QTableWidget? Because I can print off the values from that by adding print constraint_name in the same loop.
    – Joseph
    Commented May 26, 2016 at 13:36
  • 2
    A way to avoid this in the future is to not use lines like for col in [0]: # For first column "Constraint name" The comment there hints that you should define a variable named constraint_column or something and then use it in the for statement. Commented May 26, 2016 at 13:56

1 Answer 1

2

My sincere thanks to the commenters who directed me to a much more efficient solution, here is the working code which works (I'm sure it can be refined further):

qTable = self.dockwidget.tableWidget
example_group = root.findGroup('Main group')
all_items = []
gis_map = QgsMapLayerRegistry.instance().mapLayersByName( "Map" )[0]
idx = gis_map.fieldNameIndex("Rank")
for row in range(qTable.rowCount()):
    constraint_item = qTable.item(row, 0)                            
    constraint_name = str(constraint_item.text())
    item = qTable.item(row, 1)
    item_string = str(item.text())
    all_items.append(item_string)
    for group in example_group.children():
        if group.name() == "Sub group":
            if idx == -1:
                if len(set(all_items)) == 1:
                    print 'success'
                else: 
                    print 'fail'                        
            else:
                print constraint_name 

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.