Results 1 to 4 of 4

Thread: setWindowIcon() with ICO format

  1. #1
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    57
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default setWindowIcon() with ICO format

    Hello all,

    No idea whether somebody has encountered similar problem.
    I am setting the windows icon(on the top-left side of the application window) with QApplication::setWindowIcon(const QIcon&)
    The icon file which is used is ICO format under windows system.

    before I imported QT, the Windows function i used could correctly decide which image in the .ico file should be used (under proper resolution).

    Qt Code:
    1. hAppIcon = LoadIcon(Instance(), ":icons/app.ico");
    To copy to clipboard, switch view to plain text mode 

    I was expecting QIcon also do the right job, but it failed.

    Qt Code:
    1. QApplication::setWindowIcon(QIcon(QPixmap(":icons/app.ico")));
    To copy to clipboard, switch view to plain text mode 

    seems that QIcon only reads the first image from app.ico and displays it on the window.
    e.g. the first image from app.ico is with size 48*48 but actually for the window the one with size 16*16 should be used.

    by reading the documentation of QPixmap, it doesnt support .ico format. On the other hand, there are lots of sample codes on the internet in which .ico are used in QPixmap.
    It confused me so much. any ideas?

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setWindowIcon() with ICO format

    Better use this tutorial. It will definitely work. http://qt-project.org/doc/qt-4.8/appicon.html
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    57
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: setWindowIcon() with ICO format

    hello @sonulohani,

    Ive read this tutorial already. However, that actually only explains how to set up the application icon(the icon which people see on the explorer)
    which on contrary i want is window icon.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: setWindowIcon() with ICO format

    There is an ICO format image format plugin in Qt 4.8.4. I don't know exactly when it was added, but it has been around for quite a while. The code in that plugin is quite capable of loading the multiple images in ICO file but defaults to the first for non-animated formats. You can use the QImageReader directly to find the image you want. So, for this icon:
    Qt Code:
    1. $ identify qtcreator.ico
    2. qtcreator.ico[0] ICO 256x256 256x256+0+0 32-bit DirectClass 288KB 0.000u 0:00.009
    3. qtcreator.ico[1] ICO 48x48 48x48+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
    4. qtcreator.ico[2] ICO 32x32 32x32+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
    5. qtcreator.ico[3] ICO 24x24 24x24+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
    6. qtcreator.ico[4] ICO 16x16 16x16+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
    To copy to clipboard, switch view to plain text mode 
    This selects the 16x16 icon (and the first image if 16x16 is not matched):
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QImageReader reader("qtcreator.ico");
    9. QImage result = reader.read(); // this acts as a default if the size is not matched
    10. for (int i = 0; i < reader.imageCount(); ++i) {
    11. reader.jumpToImage(i);
    12. QImage image = reader.read();
    13. if (image.size() == QSize(16, 16)) {
    14. result = image;
    15. break;
    16. }
    17. }
    18.  
    19. w.setWindowIcon(QPixmap::fromImage(result));
    20. w.show();
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

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

    cic (4th July 2013)

Similar Threads

  1. setWindowIcon
    By dragon in forum Qt Tools
    Replies: 3
    Last Post: 3rd December 2012, 06:41
  2. Replies: 0
    Last Post: 13th October 2011, 16:22
  3. setWindowIcon function
    By Peppy in forum Qt Programming
    Replies: 5
    Last Post: 9th November 2009, 09:46
  4. Qt3 to Qt4 transition, setWindowIcon() problems
    By watashi16 in forum Qt Programming
    Replies: 5
    Last Post: 13th December 2007, 20:18
  5. Can setWindowIcon() work in QDockWidget?
    By alfa_wu in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2007, 08:51

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.