Results 1 to 8 of 8

Thread: How to load icons from resource file

  1. #1
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default How to load icons from resource file

    hi ,

    Could you please explain , how to use resources file .
    What i did........
    1) created a main window,
    2) added QActions on main window . and trying to put icons( .png ) on actions.
    3) created My_Resource.qrc, then added imageOne.png , imageTwo.png ...etc from the Application directory.

    see the code here, its not showing the "imageOne.png " in the QAction on mainwindow.
    Qt Code:
    1. MyMainWindow::MyMainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QAction* actNew = new QAction( QIcon("imageOne_1.png") , "&New File ..", this);
    5.  
    6. QMenu *fileMenu = menuBar()->addMenu(QObject::tr("&File"));
    7.  
    8. fileMenu->addAction( actNew );
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    please help how to load the image from the .qrc file.
    How it is different from load image directly from a realative path..??
    NB : I am using VC++ editor

    thanks

  2. #2
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to load icons from resource file

    copy the image into the application path..(the same path that contains the debug folder)

    Then if you didn't change the prefix while you added the image in My_Resource.qrc

    replace the line
    Qt Code:
    1. QAction* actNew = new QAction( QIcon("imageOne_1.png") , "&New File ..", this);
    To copy to clipboard, switch view to plain text mode 

    with this

    Qt Code:
    1. QAction* actNew = new QAction( QIcon(":/imageOne_1.png") , "&New File ..", this);
    To copy to clipboard, switch view to plain text mode 

    Please note that the name of the file has to be exact (it is case sentitive)
    Even .PNG is different from .png...so pay attention..

    at the end ..compile again and let us know

    CU

  3. #3
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: How to load icons from resource file

    I have tried this. But it's not working

    see my "My_Resource.qrc"

    [HTML]

    + /new/prefix
    |___Resources/imageOne.png

    [/HTML]

    NB: "Resources" is a directory

    Any other way..???
    thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to load icons from resource file

    Could you post the contents of My_Resource.qrc?

  5. #5
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to load icons from resource file

    create a new .qrc file from the editor

    open it

    then right clik and choose change prefix...
    write "images"

    now you will see /images appearing on the top.

    copy the file where I told you before, not under Resources,
    but at the same level as Resources..

    choose (always right click) "Add file", PNG image and IMPORT
    pick up the file and when a question is prompted to you choose NO!


    Modify the code like this:

    Qt Code:
    1. QAction* actNew = new QAction( QIcon(":/images/imageOne_1.png") , "&New File ..", this);
    To copy to clipboard, switch view to plain text mode 

    CU

  6. The following user says thank you to rmagro for this useful post:

    joseph (28th June 2007)

  7. #6
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: How to load icons from resource file

    Thank you

    I got the solution
    What i did...


    1) I have changed the prefix in .qrc file to ( Images )
    2) Then Added the images.png files from folder ( Images ) to .qrc file.
    3) Added the auto-generated "MyResourcefile.cpp" to Generated files in SolutionExplorer ( VC++ Editor )
    4) In coding ...
    Qt Code:
    1. QAction* actNew_1 = new QAction( QIcon(":Images/Resources/glass_numbers_0.png") , "&New File ..", this);
    To copy to clipboard, switch view to plain text mode 

    5) In "MyRsourcefile.qrc" which is added in Resource Files ( In Solution Explorer )

    [HTML]
    + /Images
    |_____Resources/ImageOne.png
    |_____Resources/ImageTwo.png
    |_____Resources/ImageThree.png
    [/HTML]

    NB: I think the benefit from using Resources files is..........
    1) if you are loading image directly from Imgae folder to coding ,and suppose the imageOne.png is missing in folder , UI will not able to locate the image.
    2) While using resource file , Even if the Imageone.png is missing from Image-Folder , the UI will get he image as it is added in the resources. Is it right or not ...???
    Last edited by jacek; 28th June 2007 at 17:01. Reason: moved [/html] to the right place

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to load icons from resource file

    Quote Originally Posted by joseph View Post
    NB: I think the benefit from using Resources files is..........
    1) if you are loading image directly from Imgae folder to coding ,and suppose the imageOne.png is missing in folder , UI will not able to locate the image.
    2) While using resource file , Even if the Imageone.png is missing from Image-Folder , the UI will get he image as it is added in the resources. Is it right or not ...???
    Yes, if you use resources, you simply won't lose any images, because they're part of the executable file.

  9. #8
    Join Date
    May 2012
    Location
    India
    Posts
    51
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to load icons from resource file

    What do you mean by change the prefix ??

Similar Threads

  1. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  2. A file browser that uses the native OS icons
    By calin_iaru in forum Newbie
    Replies: 1
    Last Post: 21st May 2007, 12:14
  3. Replies: 2
    Last Post: 8th March 2007, 21:47
  4. Replies: 2
    Last Post: 13th September 2006, 09:11
  5. Resource Collection File maximux size
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2006, 10:41

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.