PDA

View Full Version : Best practice naming convention for icons sets in resource file



stefanadelbert
12th May 2011, 03:26
I have a number of different sizes of each icon I use in my app all in one resource file (icons.qrc). Here is an extract from icons.qrc:


<qresource prefix="/Icons/Flags">
<file alias="Australia">Icons/Flags/24x24/Australia.png</file>
<file alias="Australia">Icons/Flags/32x32/Australia.png</file>
<file alias="Australia">Icons/Flags/48x48/Australia.png</file>
<file alias="Australia">Icons/Flags/64x64/Australia.png</file>
<file alias="Australia">Icons/Flags/128x128/Australia.png</file>
<file alias="Australia">Icons/Flags/256x256/Australia.png</file>
<file alias="USA">Icons/Flags/24x24/United-States.png</file>
<file alias="USA">Icons/Flags/32x32/United-States.png</file>
<file alias="USA">Icons/Flags/48x48/United-States.png</file>
<file alias="USA">Icons/Flags/64x64/United-States.png</file>
<file alias="USA">Icons/Flags/128x128/United-States.png</file>
<file alias="USA">Icons/Flags/256x256/United-States.png</file>
</qresource>

I initialise the resource:


Q_INIT_RESOURCE(icons);

And I use the icons in code:


countryButton->setIcon(QIcon(":/Icons/Flags/Australia"));

Everything works fine, except that since upgrading to Qt 4.7.2, I'm getting these Qt warnings at compile time:


1>Rcc'ing icons.qrc...
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'Australia'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'Australia'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'Australia'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'Australia'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'Australia'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'USA'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'USA'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'USA'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'USA'
1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icon s.qrc: Warning: potential duplicate alias detected: 'USA'

I understand why I'm getting the warnings, but I would like to know what the convention or best practice is. Ideally I don't want to have to be specific about the resolution of the icon I'm referring to in the code, i.e. I don't want to do this:


smallCountryButton->setIcon(QIcon(":/Icons/Flags/Australia_16"));
bigCountryButton->setIcon(QIcon(":/Icons/Flags/Australia_64"));

Is there something behind the scenes that will decide on the best icon to use if there are duplicate aliases?

nish
12th May 2011, 06:24
previously are you getting the correct size?

stefanadelbert
12th May 2011, 08:31
The app is working fine as it is. As far as I can tell, the correct icons are being used, although it's possible that in each case a big one is being down-scaled without me realising it.

I don't like to have warnings coming up, and the only way I can think of to get rid of the warnings is to have a unique alias for each icon in the resource file. But then I would need to be explicit in the code about which icon size to use rather than letting Qt decide.

If there are multiple image files with the same alias in a resource file, how does QIcon (or Qt in general) decide which one to use?

Lykurg
12th May 2011, 09:34
If there are multiple image files with the same alias in a resource file, how does QIcon (or Qt in general) decide which one to use?It doesn't. Imagine following code:

QPixmap pm(":/foo");
So what are you going to do with that file? How should Qt know that. Without testing I'd say, that Qt uses the last png with was defines in the qrc file. In your case the largest one. Simple change the order of the "icon sizes", rebuild your application and your icons surely doesn't look nice anymore.

stefanadelbert
26th May 2011, 02:26
I agree that Qt will probably use the last one with the given alias. That would make sense.

So, what is the best practice in this case? Each image should have a unique alias in the resource file and I need to be explicit in my code?

It would be good to know how others do this in their apps.

ChrisW67
26th May 2011, 03:08
Given that Qt was probably scaling down your largest icon and you thought it was OK why not just include the large icon and let scaling do its thing?

You could use an SVG as the single icon and let Qt scale it.

stefanadelbert
27th May 2011, 01:30
I bought an icon pack which has PNG icons. There is an SVG icon pack, but it's three times the price, so I'll just stick with the PNGs.

I tried using the 16X16 icons explicitly, but they didn't look as good as the 24x24 scaled down. So I've used 24x24 across the board and let Qt scale down where necessary. My app doesn't use larger than 24x24, so there is no scaling up required.

Thanks for the advice.

DanH
27th May 2011, 03:54
You could create one resource file for each size, and then init the correct resource file at startup.