How to specify absolute paths for QIcon

Heads up! You've already completed this tutorial.

qinhong_lai | 2020-05-28 08:19:40 UTC | #1

How to specify absolute path like self.setWindowIcon(QIcon("D://abc/"+'resource / logo.ico'))


martin | 2020-09-11 21:00:58 UTC | #2

Hey @qinhong_lai if you're building paths in Python it's a good idea to use òs.path.join to rather than joining strings together, e.g.

python
self.setWindowIcon(QIcon(
    os.path.join('D:\\', 'abc', 'resource', 'logo.ico')

That said, you really don't want to use absolute paths to point to your icons. Doing this will mean the icon must in the same location on every computer you distribute the application to.

If you're having trouble including the file with your application, take a look at this tutorial https://www.pythonguis.com/courses/packaging-and-distribution/packaging-pyqt5-pyside2-applications-windows-pyinstaller/ -- it explains a number of different ways to do it.


PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Save yourself time and frustration. Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

Book Now 60 mins ($195)

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

How to specify absolute paths for QIcon was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.