7

How can I display an image in a message box. I tried

about.setIcon(":/pics/goku3.jpg");

but it gives me errors. I know I can use the built-in about box. Here is the full code for displaying this about box.

void MainWindow::on_actionUmer_s_Program_triggered()
{
    QMessageBox about;

    about.setText("Umer's Program");
    about.setInformativeText("Copyright ; 2012 Umer Software Inc.\nI wrote this program     for fun.\n);
    about.setStandardButtons(QMessageBox::Ok);
    about.setIcon(":/pics/goku3.jpg");   // here is the error
    about.setDefaultButton(QMessageBox::Ok);
    about.show();
    about.exec();
}

Please also tell me how can set the size of that image.

Bart
  • 19,692
  • 7
  • 68
  • 77
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67

1 Answers1

12

You should not use about.setIcon(":/pics/goku3.jpg"); because the QMessageBox::setIcon(Icon) method only works with predefined icons that are

QMessageBox::NoIcon
QMessageBox::Question
QMessageBox::Information
QMessageBox::Warning
QMessageBox::Critical

To load your own picture your should use:

void setIconPixmap ( const QPixmap & pixmap )

For example:

about.setIconPixmap(QPixMap(":/pics/goku3.jpg"));

Also, if you want to use this format ":/pics/goku3.jpg" make sure your have your .qrc file (this is a resource file) configured correctly.

More information you can get from here.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Stely
  • 186
  • 1
  • 3
  • best way to resize the image itself but if you wanna do it from code then create a new pixmap QPixmap myPixmap(30,30); ( the size ) and then load the image : myPixmap.load(filename); more info here http://qt-project.org/doc/qt-4.8/qpixmap.html#pixmap-information – Stely Mar 07 '12 at 15:54