Anonymous asks
The notepad application example runs without problems on my Raspberry Pi. But the menu bar shows no icons. just blank rectangles. They do work if I click on them. What could be the problem?
Luca
This is a common issue caused by relative image paths not resolving correctly on the Raspberry Pi. You can diagnose and fix the missing icons by checking whether the QIcon is loading the image file properly.
Open the file notepad.py and go after this:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
add the following lines:
icon = QIcon(os.path.join('images', 'blue-flolder-open-document.png'))
print("availableSizes:", icon.availableSizes())
the output on the terminal should be:
availableSizes: [PyQt5.QtCore.QSize(16, 16)]
If the icon loaded correctly you'll see the size listed. However, if the output is different, for example:
availableSizes: []
this means QIcon cannot find the image file using the relative path. To fix this, replace all the occurrences of:
join('images',
with:
join(path,
where path is the absolute path to your images folder. For example:
path = '/home/USER_NAME/notepad/forum/images'
if the notepad folder is in the USER_NAME home directory.
Using an absolute path ensures that QIcon can locate the image files regardless of the working directory from which the script is launched. This is especially relevant on Raspberry Pi where the working directory may differ depending on how you start the application (e.g., from the terminal, a desktop shortcut, or a file manager).
Create GUI Applications with Python & Qt5 by Martin Fitzpatrick
(PyQt5 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!