Tuesday, 8 August 2017

Checking the opening of an attribute table with pyqgis



I know how to show the attribute table of a specific layer (with showAttributeTable), but how to check if the attribute table of a specific layer is already opened ? I don't want to show it again if it's already visible.



Answer



You could use something like the following:


from PyQt4.QtGui import QApplication

attrTables = [d for d in QApplication.instance().allWidgets() if d.objectName() in (u'QgsAttributeTableDialog', u'AttributeTable')]
for x in attrTables:
if 'layerName' in x.windowTitle():
pass
else:

# do something



Edit:


As @Etienne mentioned in the comments, you could use the following for a more accurate method of obtaining the attribute table of a specific layer (assuming no layers have duplicate names):


from PyQt4.QtGui import QApplication

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':
# do something

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...