[Qt-interest] Setting the Window Icon

Malyushytsky, Alex alex at wai.com
Wed Feb 16 19:45:41 CET 2011


Try to check if the file is actually a valid pixmap.

As far as I understand for QIcon to operate needs to have a valid QPixmap.
Since QIcon is basically a set of pixmaps, and is not a single pixmap
checking QIcon for nullness might not be sufficient to identify was picture read from the file
successfully.

According to documentation QIcon might be not null and still not be able to produce an image
for example if the file does not exist or cannot be read (or recognized?).

So I would suggest for testing purposes test if pixmap can be loaded from the file first.
You could add code similar to below to perform such check:

{
      QString path= “/path/to/file.png”;

        QPixmap pix ( path );
      Q_ASSERT( !pix.isEmpty() );
}


If assert fails, QPixmap was not able to create image from file.
And QIcon will not be able to show pixmap.

If it does not, I would check sizes of the pixmaps you load, QIconEngine might be not able to scale pixmap you load to the size you need.


Alex


From: O'carroll, David T [mailto:david.t.o'carroll at intel.com]
Sent: Wednesday, February 16, 2011 1:51 AM
To: Malyushytsky, Alex; qt-interest at qt.nokia.com
Subject: RE: Setting the Window Icon

Hi,

Thanks for the reply.
I tried checking if the file existed using the QFileInfo class and it always found the file.
I also used QDir to list the contents of the directory in which all the images I need are stored and it was able to find them all.
This is the code I’m using to check for the file:

QFileInfo file(“/path/to/file.png”);
if(file.exists())
{
                qDebug() << “Setting window icon”;
                QApplication::setWindowIcon(QIcon(“/path/to/file.png”));
}
else
{
                qDebug() << “File: “ << file.absoluteFilePath() << “ not found”;
}

This will always output “Setting window icon” and yet the icon is not always set (in fact it is quite rare that it does get set).
Seeing as the file can be read by both QFileInfo and QDir could there be a problem with QIcon?

Dave

From: qt-interest-bounces+david.t.o'carroll=intel.com at qt.nokia.com [mailto:qt-interest-bounces+david.t.o'carroll=intel.com at qt.nokia.com] On Behalf Of Malyushytsky, Alex
Sent: Tuesday, February 15, 2011 11:35 PM
To: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Setting the Window Icon

My guesses this is failure to find image file.
Try to use absolute path instead of relative and check if file exists.
Put some code which will notify you about path when  does not.

Alex


From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of O'carroll, David T
Sent: Tuesday, February 15, 2011 6:10 AM
To: qt-interest at qt.nokia.com
Subject: [Qt-interest] Setting the Window Icon

Hi,
I am trying to integrate a demo into the Meego IVI UX. I am trying to set the window icon of an application to a different image depending on what QWidget is running. The QWidget is launched as the application starts and is passed to the application as an argument (so it only needs to be set once). Currently I am setting it in main.cpp with:

QApplication::setWindowIcon(QIcon(“/path/to/image.png”));

However, this leads to somewhat random results wherein the image is set sometimes and not others. Is there something I’m missing about QIcon (I’m used to working with QPixmap) or am I simply going about this the wrong?

Regards,
Dave


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”


More information about the Qt-interest-old mailing list