|  | Home · Examples

# Qt Jambi Tutorial 1 - Hello World!

[Qt Jambi Tutorial][Next: Qt Jambi Tutorial 2 - Calling it Quits]

Code:

- HelloWorld.java

This first program is a simple "Hello world" 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.
Here's the complete source code for the application:

    package com.trolltech.examples.tutorial;

    import com.trolltech.qt.gui.*;

## Line by Line Walkthrough

    package com.trolltech.examples.tutorial;

    import com.trolltech.qt.gui.*;

To use Qt Jambi classes, you need to import at least the `gui` package, which includes the QApplication class. QApplication manages various application-wide resources and is needed to run a Qt Jambi application. The `gui` package contain GUI related classes, such as widgets, which is a user interface object that can process user input and draw graphics.

        public static void main(String args[])
        {

The `main()` method is the entry point to the program. Almost always when using Qt Jambi, `main()` 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.

The `args` parameter is the command-line arguments. This is a standard Java feature.

            QApplication.initialize(args);

Each Qt Jambi application contains a unique QApplication instance, which is a private QApplication class member. To create the instance, you call the static initialize() method with `args`; note that `args` might be changed as Qt Jambi removes command-line arguments it recognizes. See QApplication.argv() documentation for details.

The QApplication object must be created before any GUI-related features of Qt Jambi are used.

            QPushButton hello = new QPushButton("Hello World!");

Here, after the QApplication, comes the first GUI-related code: A push button is created.

QPushButton 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 QPushButton can show either a text or a QIcon.

The button is set up to display the text "Hello world!". Because we don't specify a parent window (as second argument to the QPushButton constructor), the button will be a window of its own, with its own window frame and title bar.

            hello.resize(120, 40);

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 QWidget.move() to assign a specific screen position to the widget, but instead we let the windowing system choose a position.

            hello.setWindowTitle("Hello World");

The title of the window in which the button is shown is set with QWidget.setWindowTitle().

            hello.show();

A widget is never visible when you create it. You must call QWidget.show() to make it visible.

            QApplication.exec();

        }

This is where `main()` passes control to Qt Jambi. QCoreApplication.exec() will return when the application exits. (QCoreApplication is QApplication's base class. It implements QApplication's core, non-GUI functionality and can be used when developing non-GUI applications.)

In QCoreApplication.exec(), Qt Jambi receives and processes user and system events and passes these on to the appropriate widgets.

You should now try to compile and run this program.

The tutorial examples are located in Qt Jambi's `examples/tutorial` directory. They are automatically built when you build Qt Jambi.

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 install instructions).

    javac com/trolltech/examples/tutorial/HelloWorld.java
    java com.trolltech.examples.tutorial.HelloWorld

## Running the Application

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: "Hello world!"

## Exercises

Try to resize the window. Click the button. If you're running X11, try running the program with the `-geometry` option (for example, `-geometry 100x200+10+20`).