Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 2374

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qtjambi/4.3/scripts/../doc/src/examples/resourcesystem.qdoc -->
<head>
  <title>Resource System Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Resource System Example<br /><small></small></h1>
<a name="introduction"></a>
<h2>Introduction</h2>
<p>The resource system example illustrates how you can easily bundle resources with your Java application by using the class path file engine. The class path file engine searches for the files or directories you specify in all directories or <tt>.jar</tt> files in the current class path. It also allows you to list the contents of directories in the class path, even if they are actually distributed over several different actual locations.</p>
<p>You can tell Qt to use the class path file engine when specifying the file name. Simply prefix the name with <tt>classpath:</tt>, and Qt will automatically invoke the class path file engine. If you wish to search for a file at a specific location in the class path, then a second request format is available: <tt>classpath:&lt;absolute path&gt;#&lt;filename&gt;</tt>. The first variable, the absolute path, can be the full path of a directory or <tt>.jar</tt> file, while the filename should be the resource you want to request. Qt will then only search the specific directory or <tt>.jar</tt> file for the file. This can be useful in cases when you want to work with <tt>.jar</tt> files inside of Qt.</p>
<a name="the-example"></a>
<h2>The Example</h2>
<p>The Resource System (<tt>com.trolltech.examples.ResourceSystem</tt>) example shows off the different features of the class path file engine.</p>
<p>The example has two modes:</p>
<ul>
<li>In the default mode it will list the contents of the entire class path, and in addition the contents of a bundled <tt>.jar</tt> file.</li>
<li>In the second mode it will limit itself to the contents of the <tt>.jar</tt> file.</li>
</ul>
<p>In either mode, it will show these contents in a <a href="gui/QTreeWidget.html"><tt>QTreeWidget</tt></a> and let the user select images to show them in the window. It first searches for the <tt>ResourceSystem.jar</tt> file, and gets the actual path on disk for this file by calling canonicalFilePath() on the <a href="core/QFileInfo.html"><tt>QFileInfo</tt></a> object:</p>
<pre>    String jar_file_name = &quot;classpath:com/trolltech/examples/ResourceSystem.jar&quot;;
    QFileInfo jarInfo = new QFileInfo(jar_file_name);</pre>
<p>The <tt>ResourceSystem.jar</tt> file is placed in the same directory as the currently running class file, and should be available using the path of the current package.</p>
<p>In the default mode, the application should list the contents of the entire class path. We do this by specifying the root directory and prefixing it with <tt>classpath:</tt> with the following assignment:</p>
<pre>    searchPath = &quot;classpath:/&quot;;</pre>
<p>Every entry of the class path has a root directory, so this directory is distributed over all parts of the class path; i.e&#x2e; if the classpath contains two entries: <tt>/my_java_files/:/java_sdk/rt.jar</tt>, the root directory will contain all contents of <tt>/my_java_files</tt> as well as all contents in the root of <tt>rt.jar</tt>. We simply use <a href="core/QDir.html"><tt>QDir</tt></a> on the root directory and entryList() to get its contents.</p>
<p>Since we also want the application to list the contents of <tt>ResourceSystem.jar</tt>, we will add this to the class path file engine in the default mode:</p>
<pre>    QtJambiInternal.addSearchPathForResourceEngine(jarInfo.canonicalFilePath());</pre>
<p>When the file is stored directly on the disk in the main file system, the canonical file path will return the actual, complete path and file name of the file. If it were stored within another <tt>.jar</tt> file, we wouldn't be able to add it to the class path without first extracting it (using the copy() method in <a href="core/QFile.html"><tt>QFile</tt></a>).</p>
<p>In the alternative mode, we only want to search <tt>ResourceSystem.jar</tt>. This is accomplished by requesting the root from this specific <tt>.jar</tt> file:</p>
<pre>    searchPath = &quot;classpath:&quot; + jarInfo.canonicalFilePath() + &quot;#/&quot;;</pre>
<p>After the search path has been selected, the application recursively fills the tree widget with any directory or image file it finds. From this point on it works as you would expect it to if it was a normal image viewer application that searched the main file system. When the user selects a file from the tree widget, the application will attempt to load it as a pixmap and set it on its label at the bottom of the window:</p>
<pre>    QFileInfo info = selected_item.getInfo();
    if (info.exists() &amp;&amp; !info.isDir()) {
        QPixmap pm = new QPixmap(info.absoluteFilePath());
        m_currentImage.setPixmap(pm);
    }</pre>
<a name="conclusion"></a>
<h2>Conclusion</h2>
<p>What you will see when exploring this example, is that the bundled <tt>.jar</tt> file contains a couple of image files in the path <tt>/com/trolltech/examples/images</tt>. The same path will contain an image in the Qt package (at the location in the class path where Java found the currently running class file).</p>
<p>When running in the default mode, all three images will be selectable as if they were in a single directory. In the alternative mode, only the two images contained in the <tt>.jar</tt> file are available.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>