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.

2

3 Answers 3

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
  • Thanks!, it worked like a charm. But your exportSuccess part is the icon that is the blue circled 'i' at your example.
    – BUY
    Jul 2, 2018 at 7:14
  • @BerkUtkuYenisey your welcome, yes you can change it arbitrary with your icon. Jul 2, 2018 at 7:15
  • No I have done that already :D. But which part of the code should I change to change the Icon that I have encircled with red pen at my example.(That is near the application Name)?
    – BUY
    Jul 2, 2018 at 7:16
  • @BerkUtkuYenisey its very simple, just add QPixmap then call setIconPixmap from your QMessageBox instance, finally call exec method from it. Jul 2, 2018 at 7:26
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

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.