3

I am building a plugin in Qgis 3.24 Now, I want to access the "Toggle Editing" button from the Vector ToolBar using Python. Is it possible?

Edit - I want to access rotate feature, Scale feature, Delete part etc. from the toolbar using Python.

5
  • 1
    Are you talking about the "Advanced Digitizing Toolbar" ?
    – Taras
    Nov 29, 2022 at 10:12
  • Yes , I am talking about Advanced digitizing toolbar
    – BenStone
    Nov 29, 2022 at 10:14
  • How to trigger it?
    – BenStone
    Nov 29, 2022 at 10:16
  • How do I use the 'rotate feature' after triggering the action?
    – BenStone
    Nov 29, 2022 at 10:20
  • 3
    Please avoid asking new questions in comments, Instaed, use Ask Question button to ask additional questions. You are asking "how to access rotate etc. actions". "How to use rotate etc actions" is a different question. Nov 29, 2022 at 10:29

2 Answers 2

7

The first option is simply by using the startEditing() method of the QgsVectorLayer class.

layer = iface.activeLayer()
layer.startEditing()

The second option, as was suggested by @J.Monticolo, is to apply the edit(). However, remember that it will also automatically call layer.commitChanges() on success.

layer = iface.activeLayer()
with edit(layer):
    # do something here

The third option is to penetrate the QApplication class, and try to find it's 'Digitizing Toolbar'-child.

all_widgets = QApplication.instance().allWidgets()
for widget in all_widgets:
    if widget.windowTitle() == 'Digitizing Toolbar':
        for action in widget.actions():
            if action.objectName() == 'mActionToggleEditing':
                action.trigger()

P.S. Thank you @KadirŞahbaz for a tip with trigger() method of the QAction class.


To get to the 'Rotate Feature(s)'-action from the "Advanced Digitizing Toolbar":

all_widgets = QApplication.instance().allWidgets()
for widget in all_widgets:
    if widget.windowTitle() == 'Advanced Digitizing Toolbar':
        for action in widget.actions():
            if action.objectName() == 'mActionRotateFeature':
                print(dir(action))

Please, also consider @KadirŞahbaz's answer in this thread: Calling Rotate Feature tool from QGIS Python Console

9
  • I have edited my question to exactly what i need
    – BenStone
    Nov 29, 2022 at 10:06
  • 2
    Hi Taras. setChecked doesn't activate buttons on Digitizing Toolbar, just sets the editing button checked. A better approach is to use action.trigger(). Nov 29, 2022 at 10:13
  • 2
    @BenStone If Taras' answer is helpful and solves your issue, please consider upvoting by clicking on the arrows next to the score and accepting it by clicking on the checkmark ✓. This shows others that the question is solved and will remove it from the list of unanswered questions. Nov 29, 2022 at 10:32
  • 1
    Thank you for your answer.
    – BenStone
    Nov 29, 2022 at 11:52
  • 2
    I suggest asking it as a new question, instead of editing the old question\
    – Taras
    Dec 2, 2022 at 11:14
2

A Qt answer :

edit_action = iface.mainWindow().findChild(QAction, "mActionToggleEditing")
edit_action.trigger()

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.