9

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?

1
  • Is Plugins toolbar open? Sep 19, 2020 at 15:17

1 Answer 1

8

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

1
  • feeling very daft now. Didn't realise the method added to the Plugins toolbar. Thanks Kadir
    – TrevP
    Sep 19, 2020 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.