Search code examples
pythonpyqt4qmessagebox

QMessageBox.setIcon() doesn't set the icon


I'm writing a program using PyQt4 that shows a QMessageBox to give the user a warning. I'm trying to set a default icon using setIcon(), but it doesn't show.

I'm using Python 2.7 and PyQt4 4.11.4.

Here's an example:

import sys
from PyQt4.QtGui import QApplication, QMessageBox

app = QApplication(sys.argv)
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Where is my icon?")
msg.exec_()

Am I doing anything wrong?

EDIT: as requested by @mata, here is my current output:

output with no icon

If I look for a specific image outside Qt, it works as expected:

import sys
from PyQt4.QtGui import QApplication, QMessageBox, QPixmap, QImage
from PyQt4.QtCore import Qt
import urllib

url = 'http://www.google.com/images/srpr/logo1w.png'
data = urllib.urlopen(url).read()

app = QApplication(sys.argv)
image = QImage()
image.loadFromData(data)
pixmap = QPixmap(image).scaledToHeight(32, Qt.SmoothTransformation)
msg = QMessageBox()
msg.setIconPixmap(pixmap)
msg.setText("There is an icon from the Internet here!")
msg.exec_()

And the output:

output with an icon


Solution

  • I just tried with Python 3 and PyQt5, and it works. The code changes a bit:

    import sys
    from PyQt5.QtWidgets import QApplication, QMessageBox
    
    app = QApplication(sys.argv)
    msg = QMessageBox()
    msg.setIcon(QMessageBox.Warning)
    msg.setText("Where is my icon?")
    msg.exec_()
    

    Screenshot