[Solved] Standard way to use Icons in Qt application?



  • Hi,

    I am having some problem trying to use an icon for my application.
    Since this is my first time at application development I am sorry for asking a simple question.

    I am creating the image in Inkscape. But dont know what size I should create in?
    Previously I tried using a fairly large image (200x200px JPEG format) as a resource file then use it as an icon.
    But it was giving a very bad quality.
    So my question is:

    1. What size image should I create in Inkscape?
    2. What file format should I use?
    3. Should I directly use the image or create a QIcon then use?
    4. What is the best way to have good quality icon if above mentioned approach is wrong?

    Any other suggestions are also welcome!


  • Moderators

    The most common icon size is 32x32 pixels. Automatic resizing - just as you have noticed yourself - usually results in the picture looking ugly, so it's probably best to render additional sizes manually (16x16, 64x64, maybe more if you feel like it).

    As for the extension - I would go for PNG.



  • Hi sierdzio,

    I used qApp->setWindowIcon(QIcon("<abs path to PNG file>"));
    This made the icon for main window title as well as task bar icon using my image file.

    Now, 32x32 is fine for window title bar.
    But dont you think it is less for taskbar icon?
    Is it possible to specify different image for task bar icon?

    Also, you mentioned to create "additional sizes manually (16×16, 64×64, maybe more if you feel like it).", why is it required to create these many images?

    Thanks!


  • Moderators

    For various reasons:

    • if the icon will be used somewhere inside the application, and it would be better to have it bigger (for example, Qt logo can be found in "About Qt" dialog)
    • for shortcut icons (.lnk on Windows, various other on Linux and Mac)

    You can of course keep the original, big file somewhere and create scaled down copies when they are needed. SVG and PNG are probably best options here.



  • Hi sierdzio,

    Yes thats what my big problem is!
    How to specify different images for different purposes?

    Now,
    qApp->setWindowIcon(QIcon(”<abs path to PNG file>”));
    uses the specified image file for Title bar icon, fair enough.

    But it also uses it for Task bar icon, not fair enough.
    How to specify different image file path for Task bar icon?

    Also similarly how to specify another image file for shortcut icons, which you mentioned?


  • Moderators

    I don't know about changing the taskbar icon, I've never experimented with it, to be honest. What you could try, is to set the taskbar icon with qApp as you are doing now, but then set the main window icon separately in your code (QWidget::setWindowIcon()).

    As for application shortcut, the case here is rather complicated and depends heavily on the Operating System. On Windows, you need to specify the icon in "a rc file":http://stackoverflow.com/questions/1460361/qt-creator-how-to-set-application-icon, on a Mac, you need to use info.plist file to add path to the icon file. On Linux, it gets even more challenging, because every desktop environment (KDE, Gnome, Xfce, etc.) has it's own rules. Sadly, Qt will not help you here much, you need to handle it by yourself.



  • Ohh....
    that did not come to my mind at all!
    Will try that & get back to you.
    Thanks for the additional info as well.

    Honestly speaking you have saved me from a lot of headache, as I was trying to figure out this for a pretty long time.
    Thank You...



  • [quote author="sierdzio" date="1373558808"]... As for application shortcut, the case here is rather complicated and depends heavily on the Operating System. On Windows, you need to specify the icon in "a rc file":http://stackoverflow.com/questions/1460361/qt-creator-how-to-set-application-icon, on a Mac, you need to use info.plist file to add path to the icon file. On Linux, it gets even more challenging, because every desktop environment (KDE, Gnome, Xfce, etc.) has it's own rules. Sadly, Qt will not help you here much, you need to handle it by yourself.[/quote]

    Thers is another way described "here...":http://doc-snapshot.qt-project.org/qt5-stable/qtdoc/appicon.html


  • Moderators

    It's basically the same I wrote, but the description is more elaborate and precise. Thank you for the link, I was not aware this was in the docs.



  • Strange thing just happened!

    As I told previously the setWindowIcon() was applying the specified image for title bar as well as task bar icon, looks like that is not true.
    I was running my application from Qt Creator (ctrl + r) & hence I was getting this result. But if I compile my exe & then run it manually through explorer, then only title bar icon gets generated. But task bar icon is not generated!
    Is this a bug with Qt Creator or does it works to spec?

    I guess to set the task bar icon I have to use the method for windows icon.
    @belab thanks for that link.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.