DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

21- Add a New Menu Item to QGIS Software

Learn how to customize your QGIS software and improve your workflow by adding a new menu item with PyQGIS. This step-by-step guide explains how to import the webbrowser module, define a function that opens a specified website in a web browser, create a QAction instance, and add it to the help menu. With this new menu item, you can quickly access a website of your choice directly from QGIS. Follow the instructions in this article to enhance your QGIS experience and streamline your work.

Import the webbrowser module:

import webbrowser
Enter fullscreen mode Exit fullscreen mode

Define a function that opens the specified website in a web browser:

def open_website(): webbrowser.open('https://smartrs.blog/series/pyqgis-basics')
Enter fullscreen mode Exit fullscreen mode

Create a QAction instance with the label "Go to: Introduction to PyQGIS":

website_action = QAction('Go to: Introduction to PyQGIS')
Enter fullscreen mode Exit fullscreen mode

Connect the triggered signal of the QAction to the open_website() function:

website_action.triggered.connect(open_website)
Enter fullscreen mode Exit fullscreen mode

Add a separator to the help menu:

iface.helpMenu().addSeparator()
Enter fullscreen mode Exit fullscreen mode

Add the website_action QAction to the help menu:

iface.helpMenu().addAction(website_action)
Enter fullscreen mode Exit fullscreen mode

Now, from the "Help" menu, click on the newly created item "Go to: Introduction to PyQGIS" to open the website.

Here's the overall code:

# Import the webbrowser module
import webbrowser

# Define a function that opens the specified website in a web browser
def open_website():
    webbrowser.open('https://smartrs.blog/series/pyqgis-basics')

# Create a QAction instance with the label "Go to: Introduction to PyQGIS"
website_action = QAction('Go to: Introduction to PyQGIS')

# Connect the triggered signal of the QAction to the open_website() function
website_action.triggered.connect(open_website)

# Add a separator to the help menu
iface.helpMenu().addSeparator()

# Add the website_action QAction to the help menu
iface.helpMenu().addAction(website_action)
Enter fullscreen mode Exit fullscreen mode

In conclusion, this article provides a step-by-step guide on how to add a new menu item to the QGIS software with PyQGIS. By following the instructions provided in the article, users can add a new menu item to the help menu, which will open a specified website in a web browser. This article is helpful for users who want to customize their QGIS software and improve their workflow.

If you like the content, please SUBSCRIBE to my channel for the future content

Top comments (0)