|  | Home · Overviews · Examples

# Qt Jambi Tutorial 4 - Let There Be Widgets

This example shows how to create your own widget, and describes how to control the minimum and maximum sizes of a widget.

    public class Widgets extends QWidget
    {

        public Widgets()
        {

            setFixedSize(200, 120);

            QPushButton quit = new QPushButton(tr("Quit"), this);
            quit.setGeometry(62, 40, 75, 30);
            quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));

            quit.clicked.connect(QApplication.instance(), "quit()");

            setWindowTitle(tr("Let There Be Widgets"));
        }

        public static void main(String args[])
        {
            QApplication.initialize(args);

            Widgets widget = new Widgets();
            widget.show();

            QApplication.exec();
        }

    }

## Line by Line Walkthrough

    public class Widgets extends QWidget
    {

Here we create a new class. Because this class inherits from QWidget, the new class is a widget and may be a top-level window or a child widget (like the QPushButton in the previous chapter).

        public Widgets()
        {

This class has only one member, a constructor (in addition to the members it inherits from QWidget). This widget is created without a parent. Parentless widgets are top-level windows in Qt Jambi, i.e., they are shown in separate windows on the screen. You can set a parent in the constructor of QWidget or with the QWidget.setParent() method. A widget's parent is also set to the widget to which it is added.

            setFixedSize(200, 120);

Because this widget doesn't know how to handle resizing, we fix its size. In the next chapter, we will show how a widget can respond to resize event from the user.

            QPushButton quit = new QPushButton(tr("Quit"), this);
            quit.setGeometry(62, 40, 75, 30);
            quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));

Here we create and set up a child widget of this widget (the new widget's parent is `this`, i.e. the `Widgets` instance).

The tr() method call around the string literal "Quit" marks the string for translation, making it possible to change it at run-time based on the contents of a translation file. It is a good habit to use tr() around all user-visible strings, in case you decide later to translate your application to other languages.

The QWidget.setGeometry() call sets both the widget's screen position and the size. It is equivalent to calling QWidget.move() followed by QWidget.resize(). If we didn't set the widgets geometry, the parent widget will position it using the buttons sizeHint() method - all widgets are able to calculate their preferred size.

        public static void main(String args[])
        {
            QApplication.initialize(args);

            Widgets widget = new Widgets();
            widget.show();

            QApplication.exec();
        }

Here we instantiate a Widgets, show it, and execute the application.

## Running the Application

This program is very similar in behavior to the previous one. The difference lies in the way we have implemented it. It does behave slightly differently, however. Just try to resize it to see.

## Exercises

Try to create another `Widgets` object in `main()`. What happens?

Try to add more buttons or put in other widgets than QPushButton.