4

enter image description here


Clicking "Submit" button brings a QMessageBox with three response buttons. 'Critical', 'Information', 'Question', 'Warning' boxes each have their icon. Is there a way to customize the icon on 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

alphanumeric
  • 17,967
  • 64
  • 244
  • 392

1 Answers1

8

After creating the QMessageBox, just call messagebox.setIconPixmap(QPixmap(":/images/image_file)) where image_file is the path to your image resource.

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
Monkeyanator
  • 1,346
  • 2
  • 14
  • 29
  • 2
    May want to edit the answer to include the closing quote for your relative path incase someone inexperienced copies this to try it. It should be `messagebox.setIconPixmap(QPixmap(":/images/image_file"))` Can't edit it myself because it's less than 6 characters. – PyHP3D Mar 29 '22 at 02:09