Python Forum
[PyQt] QIcon not displaying
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QIcon not displaying
#1
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?
Recommended Tutorials:
Reply
#2
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.
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#3
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?
Recommended Tutorials:
Reply
#4
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.
Reply
#5
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.
Recommended Tutorials:
Reply
#6
(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
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#7
im prolly going to move to a tile window manager soon.
Recommended Tutorials:
Reply
#8
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020