Results 1 to 8 of 8

Thread: Best practice naming convention for icons sets in resource file

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    18
    Thanked 5 Times in 5 Posts

    Default Best practice naming convention for icons sets in resource file

    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:

    Qt Code:
    1. <qresource prefix="/Icons/Flags">
    2. <file alias="Australia">Icons/Flags/24x24/Australia.png</file>
    3. <file alias="Australia">Icons/Flags/32x32/Australia.png</file>
    4. <file alias="Australia">Icons/Flags/48x48/Australia.png</file>
    5. <file alias="Australia">Icons/Flags/64x64/Australia.png</file>
    6. <file alias="Australia">Icons/Flags/128x128/Australia.png</file>
    7. <file alias="Australia">Icons/Flags/256x256/Australia.png</file>
    8. <file alias="USA">Icons/Flags/24x24/United-States.png</file>
    9. <file alias="USA">Icons/Flags/32x32/United-States.png</file>
    10. <file alias="USA">Icons/Flags/48x48/United-States.png</file>
    11. <file alias="USA">Icons/Flags/64x64/United-States.png</file>
    12. <file alias="USA">Icons/Flags/128x128/United-States.png</file>
    13. <file alias="USA">Icons/Flags/256x256/United-States.png</file>
    14. </qresource>
    To copy to clipboard, switch view to plain text mode 

    I initialise the resource:

    Qt Code:
    1. Q_INIT_RESOURCE(icons);
    To copy to clipboard, switch view to plain text mode 

    And I use the icons in code:

    Qt Code:
    1. countryButton->setIcon(QIcon(":/Icons/Flags/Australia"));
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. 1>Rcc'ing icons.qrc...
    2. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'Australia'
    3. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'Australia'
    4. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'Australia'
    5. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'Australia'
    6. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'Australia'
    7. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'USA'
    8. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'USA'
    9. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'USA'
    10. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'USA'
    11. 1>d:\dev\asap\trunk\Epoch\ASAP\Client\Resources\icons.qrc: Warning: potential duplicate alias detected: 'USA'
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. smallCountryButton->setIcon(QIcon(":/Icons/Flags/Australia_16"));
    2. bigCountryButton->setIcon(QIcon(":/Icons/Flags/Australia_64"));
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: Best practice naming convention for icons sets in resource file

    previously are you getting the correct size?

  3. #3
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    18
    Thanked 5 Times in 5 Posts

    Default Re: Best practice naming convention for icons sets in resource file

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Best practice naming convention for icons sets in resource file

    Quote Originally Posted by stefanadelbert View Post
    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:
    Qt Code:
    1. QPixmap pm(":/foo");
    To copy to clipboard, switch view to plain text mode 
    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.

  5. The following user says thank you to Lykurg for this useful post:

    stefanadelbert (27th May 2011)

  6. #5
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    18
    Thanked 5 Times in 5 Posts

    Default Re: Best practice naming convention for icons sets in resource file

    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.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Best practice naming convention for icons sets in resource file

    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    stefanadelbert (27th May 2011)

  9. #7
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    18
    Thanked 5 Times in 5 Posts

    Default Re: Best practice naming convention for icons sets in resource file

    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.

  10. #8
    Join Date
    Apr 2011
    Posts
    124
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default Re: Best practice naming convention for icons sets in resource file

    You could create one resource file for each size, and then init the correct resource file at startup.

Similar Threads

  1. Naming Convention for QT
    By metdos in forum Qt Programming
    Replies: 4
    Last Post: 20th August 2015, 10:27
  2. How to load icons from resource file
    By joseph in forum Qt Programming
    Replies: 7
    Last Post: 25th June 2012, 08:00
  3. Replies: 0
    Last Post: 21st July 2010, 11:32
  4. resource file
    By kavinsiva in forum Newbie
    Replies: 4
    Last Post: 18th August 2009, 23:28
  5. Resource file in a lib
    By Rodrigo in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2007, 16:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.