Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
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.
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)
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..
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.
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 ..