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:
<RCC>
<qresource prefix="/dir/subDir" lang="en">
<file alias="AliasName">subDir/FileName<file>
<qresource>
</RCC>
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.