3

I'm using the code in this question (Checking the opening of an attribute table with pyqgis) to check if there are attribute table open in QGIS. More precisely, I need to check if some attribute tables is showed on the screen. Unfortunately using this code tab_lyris always different from 'layer_name' because in tab_lyr there's always a space in front the real layer name. For example: ' layer_name'. Where's the problem?

attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() in (u'QgsAttributeTableDialog', u'AttributeTable')]
for x in attrTables: 
    w_title = x.windowTitle() 
    tab_lyr = w_title[:w_title.index("::")-1]
    if tab_lyr == 'layer_name':

I tried if len(attrTables)>0 but the length of attrTables can be > 0 also if there are not attribute tables shown.

This code seems to work except in some cases I haven’t figured out yet

 attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() in (u'QgsAttributeTableDialog', u'AttributeTable')]
      if len(attrTables)>0:
        for x in attrTables: 
          w_title = x.windowTitle() 
          tab_lyr = w_title[:w_title.index("::")].strip()
          print(tab_lyr)
          if tab_lyr == layer1.name():
            pass
          elif tab_lyr == layer2.name(): 
            pass
          else:
            attDialog=iface.showAttributeTable(layer1)
            attDialog.findChild(QAction,'mActionSelectedFilter').trigger()
            attDialog2=iface.showAttributeTable(layer2)
            attDialog2.findChild(QAction,'mActionSelectedFilter').trigger()   
      else: 
        attDialog=iface.showAttributeTable(layer1)
        attDialog.findChild(QAction,'mActionSelectedFilter').trigger()
        attDialog2=iface.showAttributeTable(layer2)
        attDialog2.findChild(QAction,'mActionSelectedFilter').trigger()
  • When and how do you call your function? What is Library()? This function seems to do the opposite of what you want: it opens an attribute table only if another one is already open. – Yoann Quenach de Quivillic Dec 10 '19 at 14:18
  • Ok, I corrected (sorry aboyt Library(), it was a typo it does not mean anything) – Lorenzo Dec 10 '19 at 15:10
3

The problem is that the attribute dialog window title just so happens to contain a space before the layer name

 >>> w_title
 ' layer_name :: Features Total:...'

When you slice it with w_title[:w_title.index("::")], you keep the first blank character.

Just use strip. Without arguments, strip removes whitespace characters from the start and the end of your string

tab_lyr = w_title[:w_title.index("::")].strip()

To answer to the second part of your question, your poblem is that when you have only one atttribute table opened (either layer1 or layer2), you never open the other. Here is a more generic solution. If you need to handle more layers, just replace (layer1, layer2) with (layer1, layer2, layer3, ...) and so on.

attribute_table_titles = [
    d.windowTitle()
    for d in QApplication.instance().allWidgets()
    if d.objectName() in (u"QgsAttributeTableDialog", u"AttributeTable")
]

already_open_layers = [
    title[: title.index("::")].strip() for title in attribute_table_titles
]

for layer in (layer1, layer2):
    if layer.name() not in already_open_layers:
        attDialog = iface.showAttributeTable(layer)
        attDialog.findChild(QAction, "mActionSelectedFilter").trigger()
| improve this answer | |
  • Hi thanks for your answer! Can you check my edit? – Lorenzo Dec 10 '19 at 12:39
  • It works very well, thanks! – Lorenzo Dec 10 '19 at 16:09

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.