Home > Programming > Java, C++: ClassLoader and Qt Resource System

Java, C++: ClassLoader and Qt Resource System

Here a little comparison between the Java ClassLoader and the Qt Resource System:
If you want to load an icon and/or an image e.g. in a Java .jar-file you have to do it this way:

Icon icon;
Image image;
ClassLoader CLDR = this.getClass().getClassLoader();
String path = "src/to/images/name.png";
URL url = CLDR.getResource(path);
// An icon
if (!(url == null)) {
    icon = new ImageIcon(url);
// An image
try {
    image = ImageIO.read(CLDR.getResource(path));
} catch (IOException e) {
    // ExceptionHandling here

In Qt there is another approach:
The resources associated with an application are specified in a XML-based .qrc-file.
The specified paths are relative to the directory containing the .qrc-file (listed files must be located in the same directory or any sub-directory).
In the source-code the resources are accessible under the same name they have in the source tree, with a “:/” prefix.
Here an example:

    <qresource prefix="/dir/subDir" lang="en">
        <file alias="AliasName">subDir/FileName<file>

In your source code you can access FileName (if FileName e.g. is a QFile) via

QFile file(":/dir/subdir/AliasName");

after adding the following line to your .pro-file:

RESOURCES = yourRsrcFileName.qrc

The option lang=”langCode” (here: “en”) is for the localization:
If you want to add another language to your application you have to have the file “AliasName_langCode” in your source tree.

And, of course, you don’t have to write the ressource file on your own if you use the QtCreator, there is a nice GUI included.

About these ads
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: