1

Since QGIS 3, there are two vertex tools: current layer and new: all layers. Normally I was able to access all actions with the code below. But now I can't find and activate the vertex tool (current layer). Even not by searching its caption! I have no idea how an action could be added which "does not exist". Any ideas?

def findAction(objName):
    try:
        toolbars = iface.mainWindow().findChildren(QToolBar)
        for toolbar in toolbars:
            for action in toolbar.actions():
                if isinstance(action, QWidgetAction):
                    if action.defaultWidget() and action.defaultWidget().actions():
                        dwa = action.defaultWidget().actions()
                        for a in dwa:
                            if IsSearchedAction(a,objName):
                                print ("a",a.objectName())
                                return a
                else:
                    if IsSearchedAction(action, objName): 
                        print ("action",action.objectName())
                        return action
        menubar = iface.mainWindow().menuBar()
        for action in menubar.actions():
            if action.menu():
                for action in action.menu().actions():
                    print("menu")
                    if action.menu():
                        for a in action.menu().actions():
                            if IsSearchedAction(a, objName): return a
                    else:
                        if IsSearchedAction(action, objName): return action
            else:
                if IsSearchedAction(action, objName): return action
        return None
    except Exception as e:
        print (e)

def IsSearchedAction(action, objname):
    objname = objname.lower()
    caption = action.text()
    caption = caption.replace("&", "")
    caption = caption.replace(".", "")
    caption = caption.strip()
    caption= caption.lower()
    return action.objectName().lower() == objname or caption == objname

print (iface.mapCanvas().mapTool().action().objectName())
action ="mActionVertexToolActiveLayer"
action ="mActionMeasure"
action ="mActionSelectFeatures"
action ="mActionVertexToolActiveLayer"#not found
print(findAction(action))
1
def findActionByObjectName(objname):
    try:
        toolbars = iface.mainWindow().findChildren(QToolBar)
        for toolbar in toolbars:
            for action in toolbar.actions():
                if action.objectName():
                   if action.objectName().lower() == objname.lower(): return action
                if action.actionGroup() is not None:
                    for a in action.actionGroup().actions():
                        if a.objectName():
                            if a.objectName().lower() == objname.lower(): return a
        menubar = iface.mainWindow().menuBar()
        for action in menubar.actions():
            if action.menu():
                for action in action.menu().actions():
                    if action.menu():
                        for a in action.menu().actions():
                            if a.objectName():
                                if a.objectName().lower() == objname.lower(): return a
                    else:
                        if action.objectName():
                            if action.objectName().lower() == objname.lower(): return action
            else:
                if action.objectName():
                    if action.objectName().lower() == objname.lower(): return action
    except Exception as e:
        print(e)

def findActionByLabel(label):
    try:
        label = label.lower()
        toolbars = iface.mainWindow().findChildren(QToolBar)
        for toolbar in toolbars:
            for action in toolbar.actions():
                if action.text():
                   if action.text().lower() == label: return action
                if action.actionGroup() is not None:
                    for a in action.actionGroup().actions():
                        if a.text():
                            if a.text().lower() == label: return a
        menubar = iface.mainWindow().menuBar()
        for action in menubar.actions():
            if action.menu():
                for action in action.menu().actions():
                    if action.menu():
                        for a in action.menu().actions():
                            if a.text():
                                if a.text().lower() == label: return a
                    else:
                        if action.text():
                            if action.text().lower() == label: return action
            else:
                if action.text():
                    if action.text().lower() == label: return action
    except Exception as e:
        print(e)

actions = ["mActionVertexToolActiveLayer","mActionMeasureArea","mActionSelectFeatures","mActionVertexToolActiveLayer","X", "Enable Tracing","EnableTracingAction"]
for action in actions:
    found =findActionByObjectName(action)
    if found:
        print ("objectName:",found.objectName())
    else:
        found = findActionByLabel(action)
        print ("===")
        if found:
            print (found.text(),found)
            if found.objectName():
                print (found.text(),"/",found.objectName())
        else:
            print ("NOT FOUND:",action)
        print ("===")

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.