does not show icons

Viewed 17478

I just installed Python3 (3.5.2) and Pyqt5 (5.8.2) and I am following this tutorial to learn and make a GUI: http://zetcode.com/gui/pyqt5/firstprograms/

I'm trying to run the 2nd example but program is returning an error (which also happened on the 1st one, but since it had no image i took no notice) which is the following:

QApplication: invalid style override passed, ignoring it.
No XVisualInfo for format QSurfaceFormat(version 2.0, options QFlags<QSurfaceFormat::FormatOption>(), depthBufferSize -1, redBufferSize 1, greenBufferSize 1, blueBufferSize 1, alphaBufferSize -1, stencilBufferSize -1, samples -1, swapBehavior QSurfaceFormat::SwapBehavior(SingleBuffer), swapInterval 1, profile  QSurfaceFormat::OpenGLContextProfile(NoProfile))
Falling back to using screens root_visual.

What is the meaning of this? Am i missing some packages?

I installed pyqt first with this command:

sudo -H pip3 install PyQt5

but Python3 was not acknowledging its existence so i searched the apt ubuntu repos and installed with:

sudo apt install python3-PyQt5

I also tried to reference the image by full path /foo/bar/image.png and nothing

What is the problem?

EDIT #1

The code that i am using is from example 2:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

This example shows an icon
in the titlebar of the window.

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

base_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(base_dir)

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('image.png'))

        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

After your post i reinstalled all my packages. The error is slightly different but the result is the same:

python3 example_02.py 
QApplication: invalid style override passed, ignoring it.

Screencapture:Screenshot of my result

6 Answers

I study PyQt5 from author who give this question,also I have this problem that my icons can't show,I try some ways to catch it,that's what I do,hope it works!

First, it's important that you should use absolute path of the icons,for example:

self.setWindowIcon(QIcon("F:/Workspace/PyQT5-Study/images/web.png"))

but this not a good idea,so you can use second way like this:

from PyQt5.QtCore import QFileInfo
# ...
def initUI(self):
    # ...
    root = QFileInfo(__file__).absolutePath()
    self.setWindowIcon(QIcon(root+'/images/web.png'))
    # or
    # import os
    # current_dir = os.path.dirname(os.path.realpath(__file__))
    # self.setWindowIcon(QIcon(os.path.join(current_dir, 'images/web.png')))
    # ...

Last, if your icons also can't show, you should check this icon, if it's a legal icon.

In short, the normal images are unlimited so more, they can store many images and transform easily, but the icons have sure size,color kind,and more important,the icons have transparency, that means you can see the background, they have frame(not always straight). So you can use the web online tools to transform your image and try again,that really help me!

Also you should check the icon's source format, ensure you never change it, like .jpg to .png,and other. This will produce problem! Wish you can solve the problem!

On windows be sure to use a real .ico file and a full path

iconpath = os.path.join(os.getcwd(),'qtlearning','assets','python.ico')  
self.setWindowIcon(QIcon(iconpath))

I faced the exact same problem.

First things first. There is no setWindowIcon() method under QWidget or QMainWindow classes, in fact. you should be trying to set the QIcon at the Application level as follows.

app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('home.png'))

Second, the icon thus created using this code does not reflect on the title of the window, instead it will reflect as an application icon as shown in the image below. the home.png

https://i.stack.imgur.com/XdqTR.png

" icon for Application" in Ubuntu and not the " icon over the Window Title"

Finally, the path does not really matter, it can be an absolute path or a relative path, the system will consider either.

i just provided the full path of icon as simple as that

Related