TransWikia.com

Adding custom toolbar icons in PyQGIS

Geographic Information Systems Asked on February 15, 2021

I am following a tutorial at https://courses.spatialthoughts.com/pyqgis-in-a-day.html
that gives some instructions on how to set up a new toolbar item with a custom icon in a QGIS map using PyQGIS in the integrated python console

import os
from datetime import datetime

icon = 'question.svg'
data_dir = os.path.join(os.path.expanduser('~'), 'Downloads/pyqgis_in_a_day/')
icon_path = os.path.join(data_dir, icon)
print(icon_path)

def show_time():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    iface.messageBar().pushMessage('Time is {}'.format(current_time))
    
action = QAction('Show Time')
action.triggered.connect(show_time)
action.setIcon(QIcon(icon_path))
iface.addToolBarIcon(action)

The code runs without error but no icon gets placed in the toolbar area of QGIS
I have checked the path to the icon and tested that also by supplying the full path, copy and paste, to avoid typos but no success. I am using QGIS version 3.14 and checked over the documentation and the methods seem to be correctly

Can anyone see what I might be doing wrong?

One Answer

iface.addToolBarIcon method adds an icon to the Plugins toolbar. It doesn't add the icon to the toolbar area. You should open Plugins toolbar. (3rd one is Show Time icon in the image)

enter image description here

Correct answer by Kadir Şahbaz on February 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP