9

I am creating a ui app with Qt c++.

I have a error message that I have created by using QMessageBox Class like :

QMessageBox errorMessage;
errorMessage.critical(0, "Error", "An error has occured !");
errorMessage.setFixedSize(500, 200);

It is like:

enter image description here

And I want to change the red circled things which are the icon and the title.

enter image description here

I would appreciate any help.

Thanks in advance.

BUY
  • 705
  • 1
  • 11
  • 30
  • http://doc.qt.io/qt-5/qmessagebox.html#icon-prop, http://doc.qt.io/qt-5/qmessagebox.html#iconPixmap-prop – agent_bean Jul 02 '18 at 07:01
  • I have already checked the documentation before asking the question, I would be glad if you can provide me with a working code snippet because it is not working when I use setIcon or setWindowIcon or setPixmap etc. – BUY Jul 02 '18 at 07:07

3 Answers3

6

However you can use of QMessageBox::critical(0, "Error", "An error has occured !"); because critical(...) method is static and theres no need for create an instance of QMessageBox.

Use bellow code :

QMessageBox mb("Application Name",
                           "Hardware failure.\n\nDisk error detected\nDo you want to stop?",
                           QMessageBox::NoIcon,
                           QMessageBox::Yes | QMessageBox::Default,
                           QMessageBox::NoButton,
                           QMessageBox::NoButton);

QPixmap exportSuccess("/media/msi/Information/Pictures/Icons/Icons Pack/PNG/48X48/about.png");
mb.setIconPixmap(exportSuccess);
mb.exec();

enter image description here

This example work 100%

4

If you want to set the icon for every window you can do so globally with this:

QApplication::setWindowIcon(QIcon(":/GuiMain/gh_resource/GH Icon.ico"));

You also aren't required to use pixmap in my experience, you can use:

mb.setWindowIcon(QIcon(":/path/to/icon.ico"));

This will work on individual message boxes, just add your custom icon to your .qrc file

GuidedHacking
  • 3,628
  • 1
  • 9
  • 59
3

Simply you can set Icon for your Application it will automatically set on your MessegeBox

Dnyaneshwar
  • 122
  • 1
  • 3
  • 11