You are not logged in.

1

Monday, September 6th 2010, 3:07pm

QMessageBox setWindowIcon

Hi,

I have a QMessageBox.
It is QMessageBox::question(0, "WindowTitle","Do you want to Proceed?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No);

By default this QMessageBox has some icon on its window. How can I customize or set different image as its windowicon??

I tried setWindowIcon(QIcon("image.jpg")); it didnt work for me.

Please let me know how do I set the windowicon for this messagebox.

Thanks.

2

Monday, September 6th 2010, 4:14pm

You can't do that with QMessageBox static methods.

You have to instantiate a QMessageBox object (see documentation examples)

This post has been edited 1 times, last edit by "alexisdm" (Sep 7th 2010, 9:17am)


3

Tuesday, September 7th 2010, 7:22am

hi..Thanks for the reply..

so the code for that looks like..

QMessageBox msgBox;
msgBox.setWindowIcon(QIcon("image.jpg));
msgBox.question(0, "WindowTitle","Do you want to Proceed?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No);


I tried QMessageBox::Question static method and then i used msgBox.setWindowIcon(....) but it is not showing the change..

4

Tuesday, September 7th 2010, 9:48am

Hi,

You are still using a static method (QMessageBox::question) and the file path won't always work, you should either put the file in a Qt resource file (and use ":/image.jpg") or in the executable directory (and use QApplication::applicationDirPath()+"/image.jpg"), or a search prefix as in the code below:

Source code

1
2
3
4
5
6
7
// In the main function
QDir::setSearchPaths("images", QStringList(QApplication::applicationDirPath()));
...
QMessageBox msgBox(QMessageBox::Question, "WindowTitle","Do you want to Proceed?",QMessageBox::Yes | QMessageBox::No, 0 );
msgBox.setWindowIcon(QPixmap("images:image.jpg"));
msgBox.setDefaultButton(QMessageBox::No);
msgBox.exec();


And you should probably not use a JPG file as an icon because it doesn't support transparency.

5

Wednesday, September 8th 2010, 4:35pm

Thanks a lot.. It worked for me..

I tried this..

QIcon icon("C:/folder/image.png");
QMessageBox msgBox(QMessageBox::Question, "Title","Are you sure you want to Proceed?",QMessageBox::Yes | QMessageBox::No, 0 );
msgBox.setWindowIcon(icon);
msgBox.setDefaultButton(QMessageBox::No);
msgBox.exec();

This code worked ..