Solved Icons not displaying in QToolBar
-
Hello,
I'm a beginner on PyQt5 and i'm trying to display my first window with a toolBar and an icon inside. In the Qt Designer i just add a QtAction with an icon and it is visible correclty there but when i lauch the script the icon is not displayed, only the button text.
MainWindow class :
class Ui_mainWindow(object): def setupUi(self, mainWindow): mainWindow.setObjectName("mainWindow") mainWindow.resize(800, 600) mainWindow.setMinimumSize(QtCore.QSize(800, 600)) mainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor)) mainWindow.setMouseTracking(False) mainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon) self.centralwidget = QtWidgets.QWidget(mainWindow) self.centralwidget.setObjectName("centralwidget") mainWindow.setCentralWidget(self.centralwidget) self.toolBar = QtWidgets.QToolBar(mainWindow) self.toolBar.setMovable(False) self.toolBar.setAllowedAreas(QtCore.Qt.AllToolBarAreas) self.toolBar.setIconSize(QtCore.QSize(30, 30)) self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon) self.toolBar.setFloatable(True) self.toolBar.setObjectName("toolBar") mainWindow.addToolBar(QtCore.Qt.LeftToolBarArea, self.toolBar) self.actionNewFile = QtWidgets.QAction(mainWindow) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("ressources/icons/download.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.actionNewFile.setIcon(icon) self.actionNewFile.setShortcutContext(QtCore.Qt.WidgetShortcut) self.actionNewFile.setVisible(True) self.actionNewFile.setShortcutVisibleInContextMenu(True) self.actionNewFile.setObjectName("actionNewFile") self.toolBar.addAction(self.actionNewFile) self.retranslateUi(mainWindow) QtCore.QMetaObject.connectSlotsByName(mainWindow)
Main script :
import sys from PyQt5 import QtWidgets from form import Ui_mainWindow class ExampleApp(QtWidgets.QMainWindow, Ui_mainWindow): def __init__(self): super().__init__() self.setupUi(self) def main(): app = QtWidgets.QApplication(sys.argv) window = ExampleApp() window.show() app.exec_() if __name__ == '__main__': main()
Repositories architecture :
QTProject\
_form.py
_main.py
_ressources\
__icons\
___download.pngThank you for your help.
-
@ptrs32 said in Icons not displaying in QToolBar:
icon.addPixmap(QtGui.QPixmap("ressources/icons/download.png")
One thought: that is a relative path. It will be sought relative to whatever the current/working directory when you run the Python script. I don't know what that might be for you, depending on how you run it from where?
You can test something like
pixmap = QtGui.QPixmap("ressources/icons/download.png") print(pixmap.isNull())
or similar to verify whether that file was read in.
I assume
ressources
is correct name for you, because it's a misspelling ofresources
. That's OK so long as it is what you have. You could also try something likeopen("ressources/icons/download.png", "rb")
in Python to see whether the file path is found/can be opened, independent of whether it works as a pixmap.
-
@ptrs32 said in Icons not displaying in QToolBar:
icon.addPixmap(QtGui.QPixmap("ressources/icons/download.png")
One thought: that is a relative path. It will be sought relative to whatever the current/working directory when you run the Python script. I don't know what that might be for you, depending on how you run it from where?
You can test something like
pixmap = QtGui.QPixmap("ressources/icons/download.png") print(pixmap.isNull())
or similar to verify whether that file was read in.
I assume
ressources
is correct name for you, because it's a misspelling ofresources
. That's OK so long as it is what you have. You could also try something likeopen("ressources/icons/download.png", "rb")
in Python to see whether the file path is found/can be opened, independent of whether it works as a pixmap.
-
@JonB Ok i found the problem. I renamed the "ressources" repository in "resources" and it works... I didn't know the name of this repository was really important as long as my paths was correct, but apparently yes.
So thank you and have a good day :)