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 Answer 1
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)
-
feeling very daft now. Didn't realise the method added to the Plugins toolbar. Thanks KadirTrevP– TrevP2020年09月19日 16:35:38 +00:00Commented Sep 19, 2020 at 16:35
Plugins
toolbar open?