Search code examples
c++qtmessagebox

Displaying Image in QmessageBox


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.


Solution

  • 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.