Important: Please read the Qt Code of Conduct -

Set Window Icon in QMessageBox

  • Hi,

    I would like to set an icon for the window of QMessageBox
    Below is the code :

    QMessageBox::StandardButton popupYesNoBox(QWidget *parent, const QString &message, const QString &title)
      QMessageBox qmsg;
      return qmsg.question(parent, title, message, QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No));

    When I uncomment the second line, it says "cannot convert from 'const char [12]' to 'QIcon'
    Source or target has incomplete type"
    I am using the same icon, in FileDialogs its working fine.

    Kindly help.

    You're not getting the right overload of the QIcon() constructor.

    In any case (not that I know about this) the examples I see for .png convert it to QPixmap first. This is supposed to work:

    QPixmap pixmap = QPixmap(":/image.png");

  • Hi,

    No QPixelMap doesn't work.
    I am calling the above function popupYesNoBox from another class with QWidget *parent as NULL.

    auto response = nw::popupYesNoBox(NULL, tr("Previous copy exists "
                                                       "\n\n Select No"
                                                       "\n\n Select Yes"), tr("Backup file "));

    Is there something i could do by setting the parent?

  • @MTOLANI said in Set Window Icon in QMessageBox:

    I am using the same icon, in FileDialogs its working fine.

    Could you post that code snippet?

    For me, it looks like you're forgetting the #include <QIcon> in the file that holds popupYesNoBox() method.

Log in to reply