Sophie

Sophie

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

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/tutorial.qdoc -->
<head>
  <title>Qt Jambi Tutorial 1 - Hello World!</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">Qt Jambi Tutorial 1 - Hello World!<br /><small></small></h1>
<p>This first program is a simple &quot;Hello world&quot; example. It contains only the bare minimum you need to get a Qt Jambi application up and running. The picture below is a screenshot of this program.</p>
<p align="center"><img src="images/tutorial1-example.png" alt="Screenshot of Chapter 1" /></p><p>Here's the complete source code for the application:</p>
<pre>    public class HelloWorld
    {
        public static void main(String args[])
        {
            QApplication.initialize(args);

            QPushButton hello = new QPushButton(&quot;Hello World!&quot;);
            hello.resize(120, 40);
            hello.setWindowTitle(&quot;Hello World&quot;);
            hello.show();

            QApplication.exec();
        }
    }</pre>
<a name="line-by-line-walkthrough"></a>
<h2>Line by Line Walkthrough</h2>
<pre>    package com.trolltech.examples.tutorial;

    import com.trolltech.qt.gui.*;</pre>
<p>To use Qt Jambi classes, you need to import at least the<tt>gui</tt> package, which includes the <a href="gui/QApplication.html"><tt>QApplication</tt></a> class. <a href="gui/QApplication.html"><tt>QApplication</tt></a> manages various application-wide resources and is needed to run a Qt Jambi application. The <tt>gui</tt> package contain GUI related classes, such as widgets, which is a user interface object that can process user input and draw graphics.</p>
<pre>        public static void main(String args[])
        {</pre>
<p>The <tt>main()</tt> function is the entry point to the program. Almost always when using Qt Jambi, <tt>main()</tt> only needs to perform some kind of initialization before passing the control to the Qt Jambi library, which then tells the program about the user's actions.</p>
<p>The <tt>args</tt> parameter is the command-line arguments. This is a standard Java feature.</p>
<pre>            QApplication.initialize(args);</pre>
<p>Each Qt Jambi application contains a unique <a href="gui/QApplication.html"><tt>QApplication</tt></a> instance, which is a private <a href="gui/QApplication.html"><tt>QApplication</tt></a> class member. To create the instance, you call the static <a href="porting4.html#qapplication">initialize()</a> method with <tt>args</tt>; note that <tt>args</tt> might be changed as Qt Jambi removes command-line arguments it recognizes. See <a href="gui/QApplication.html"><tt>QApplication</tt></a>.argv() documentation for details.</p>
<p>The <a href="gui/QApplication.html"><tt>QApplication</tt></a> object must be created before any GUI-related features of Qt Jambi are used.</p>
<pre>            QPushButton hello = new QPushButton(&quot;Hello World!&quot;);</pre>
<p>Here, after the <a href="gui/QApplication.html"><tt>QApplication</tt></a>, comes the first GUI-related code: A push button is created.</p>
<p><a href="gui/QPushButton.html"><tt>QPushButton</tt></a> is a GUI push button that the user can press and release. The programmer can change both the overall look and feel and many minor properties of it (such as color), as well as the widget's content. A <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> can show either a text or a <a href="gui/QIcon.html"><tt>QIcon</tt></a>.</p>
<p>The button is set up to display the text &quot;Hello world!&quot;. Because we don't specify a parent window (as second argument to the <a href="gui/QPushButton.html"><tt>QPushButton</tt></a> constructor), the button will be a window of its own, with its own window frame and title bar.</p>
<pre>            hello.resize(120, 40);</pre>
<p>The button is set up to be 120 pixels wide and 40 pixels high (excluding the window frame, which is provided by the windowing system). We could call <a href="gui/QWidget.html"><tt>QWidget</tt></a>.move() to assign a specific screen position to the widget, but instead we let the windowing system choose a position.</p>
<pre>            hello.setWindowTitle(&quot;Hello World&quot;);</pre>
<p>The title of the window in which the button is shown is set with <a href="gui/QWidget.html"><tt>QWidget</tt></a>.setWindowTitle().</p>
<pre>            hello.show();</pre>
<p>A widget is never visible when you create it. You must call <a href="gui/QWidget.html"><tt>QWidget</tt></a>.show() to make it visible.</p>
<pre>            QApplication.exec();
        }</pre>
<p>This is where <tt>main()</tt> passes control to Qt Jambi. <a href="core/QCoreApplication.html"><tt>QCoreApplication</tt></a>.exec() will return when the application exits. (<a href="core/QCoreApplication.html"><tt>QCoreApplication</tt></a> is <a href="gui/QApplication.html"><tt>QApplication</tt></a>'s base class. It implements <a href="gui/QApplication.html"><tt>QApplication</tt></a>'s core, non-GUI functionality and can be used when developing non-GUI applications.)</p>
<p>In <a href="core/QCoreApplication.html"><tt>QCoreApplication</tt></a>.exec(), Qt Jambi receives and processes user and system events and passes these on to the appropriate widgets.</p>
<p>You should now try to compile and run this program.</p>
<p>The tutorial examples are located in Qt Jambi's <tt>examples/tutorial</tt> directory. They are automatically built when you build Qt Jambi.</p>
<p>If you have typed in the source code manually, you compile and run it as a regular Java program (provided that you have set up Qt Jambi correctly, see the <a href="qtjambi-installation">install instructions</tt></a>).</p>
<pre>    javac com/trolltech/examples/tutorial/HelloWorld.java
    java com.trolltech.examples.tutorial.HelloWorld.java</pre>
<a name="running-the-application"></a>
<h2>Running the Application</h2>
<p>When you run the application, you will see a small window filled with a single button, and on it you can read the famous words: &quot;Hello world!&quot;</p>
<a name="exercises"></a>
<h2>Exercises</h2>
<p>Try to resize the window. Click the button. If you're running X11, try running the program with the <tt>-geometry</tt> option (for example, <tt>-geometry 100x200+10+20</tt>).</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>