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?

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Sep 19, 2020 at 13:21
1
  • Is Plugins toolbar open? Commented 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.