通过PyQGIS添加工具栏?


10

通过教程,我学习了如何通过python将工具按钮添加到plugins-toolbar。现在,我想知道如何通过python添加带有工具栏按钮的完整工具栏。

有人可以提供示例代码吗?

Answers:


17

您可以通过QgisInterface(即iface)使用addToolBar()API调用来创建自定义工具栏:

class MyPlugin:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface

    def initGui(self):
        # Add toolbar 
        self.toolbar = self.iface.addToolBar("My_ToolBar")

        # Create actions 
        self.someact = QAction(QIcon(":/plugins/MyPlugin/icons/someactionicon.png"),
                               QCoreApplication.translate("MyPlugin", "My Action"),
                               self.iface.mainWindow())

        # Connect action signals to slots
        self.someact.triggered.connect(self.doSomething)

        # Add actions to the toolbar
        self.toolbar.addAction(self.someact)

    def unload(self):
        # remove toolbar on plugin unload
        del self.toolbar

    def doSomething(self):
        # slot for action
        pass

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.