Python Forum

Full Version: QIcon not displaying
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im running through PyQt5 tuts and came across a couple of tutorials that use QIcon. I cant get anything given to QIcon to work at all whether its through QAction or setting the window icon via...

self.setWindowIcon(QtGui.QIcon('py.png'))   
I tried the following based on google search results:
  • full path
  • changing image formats
  • changing image sizes
  • image in same directory as script


full code:
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QFile


class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):               
        print(QFile.exists('py.png'))
        exitAction = QAction(QIcon('/home/metulburr/py.png'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)
        
        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Menubar')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  
Anything given to QIcon is as if the line didnt exist at all. 

Ubuntu 16.04 
My system was acting wacky so i did a complete fresh install again. Maybe i am missing a dependency or something?
Not to rub salt in the wound, but your code works perfectly on my system (Debian Sid). I'm running the script from my Documents folder and the image in in ~/Pictures.

The only change I made was providing a full path to
print(QFile.exists('py.png'))
:
print(QFile.exists('/home/fred/Pictures/Adam.png'))
I wonder what would happen if you used os.path.asbpath? Something like
from os.path import abspath
img_path = abspath('/home/metulburr/py.png')
exitAction = QAction(QIcon(img_path), '&Exit', self)
It ought not change anything but you never know.
Quote:I wonder what would happen if you used os.path.asbpath?
Notta. But now i am wondering if it is a desktop issue. What desktop are you using?
Works fine for my with .png in same folder as scripts in both Win-10 and Mint 18.1.
metulburr Wrote:My system was acting wacky so i did a complete fresh install again. Maybe i am missing a dependency or something?
Virtual environment Wink 
I run PyQt5 in virtual environment in both Win-10 and Mint,not gone install it in OS python.
Its got to be an issue with Ubuntu's Unity then because as soon as i installed another desktop it started working now problem. Another tally for it heading out.
(May-18-2017, 11:56 AM)metulburr Wrote: [ -> ]Notta. But now i am wondering if it is a desktop issue. What desktop are you using?

MATE 1.16.1
im prolly going to move to a tile window manager soon.
(May-18-2017, 04:35 PM)metulburr Wrote: [ -> ]Its got to be an issue with Ubuntu's Unity
Unity is dead,so best to switch.