如何将自定义图像放在QMessageBox上

时间:2016-05-13 04:34:58

标签: python qt pyqt qmessagebox

enter image description here

点击“提交”按钮会带来一个带有三个响应按钮的QMessageBox。 “严重”,“信息”,“问题”,“警告”框都有自己的图标。有没有办法在QMessageBox上自定义图标?

from PyQt4 import QtGui, QtCore
import sys
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        self.resize(300, 100)
        self.setLayout(QtGui.QVBoxLayout())

        button = QtGui.QPushButton('Submit')
        button.clicked.connect(self.onclick)
        self.layout().addWidget(button)

    def onclick(self):
        self.close()
        messagebox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "Title text", "body text", buttons = QtGui.QMessageBox.Discard | QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Ok, parent=self)
        messagebox.setDefaultButton(QtGui.QMessageBox.Cancel)
        exe = messagebox.exec_()
        print 'messagebox.exec_(): %s'%exe    



dialog = Dialog()
dialog.show()     
app.exec_()

enter image description here

1 个答案:

答案 0 :(得分:2)

创建QMessageBox后,只需致电messagebox.setIconPixmap(QPixmap(":/images/image_file)),其中image_file是图片资源的路径。