Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Icons not displaying in QToolBar

Icons not displaying in QToolBar

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ptrs32
    wrote on last edited by ptrs32
    #1

    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.png

    Thank you for your help.

    JonBJ 1 Reply Last reply
    0
    • P ptrs32

      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.png

      Thank you for your help.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 of resources. That's OK so long as it is what you have. You could also try something like

      open("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.

      P 1 Reply Last reply
      1
      • JonBJ JonB

        @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 of resources. That's OK so long as it is what you have. You could also try something like

        open("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.

        P Offline
        P Offline
        ptrs32
        wrote on last edited by
        #3

        @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 :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved