QMessageBox.setIcon()没有设置图标

时间:2017-02-15 11:30:48

标签: python pyqt4 qmessagebox

我正在使用PyQt4编写程序,该程序显示QMessageBox以向用户发出警告。我尝试使用setIcon()设置默认图标,但它没有显示。

我使用的是Python 2.7和PyQt4 4.11.4。

以下是一个例子:

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_()

我做错了吗?

编辑:按照mata的要求,这是我当前的输出:

output with no icon

如果我在Qt之外寻找特定图像,它会按预期工作:

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_()

输出:

output with an icon

1 个答案:

答案 0 :(得分:1)

我刚尝试使用Python 3和PyQt5,它可以工作。代码稍有变化:

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