|  | Home · Examples

# Qt Jambi Tutorial 5 - Building Blocks

[Previous: Qt Jambi Tutorial 4 - Let There Be Widgets][Qt Jambi Tutorial][Next: Qt Jambi Tutorial 6 - Building Blocks Galore!]

Code:

- Blocks.java

This example shows how to create and connect together several widgets by using signals and slots, and how to handle resizes.

    public class Blocks extends QWidget
    {

        public Blocks()
        {
            QPushButton quit = new QPushButton(tr("Quit"));
            quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));

            QLCDNumber lcd = new QLCDNumber(2);
            lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);

            QSlider slider = new QSlider(Qt.Orientation.Horizontal);

            slider.setRange(0, 99);

            slider.setValue(0);

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

            slider.valueChanged.connect(lcd, "display(int)");

            QVBoxLayout layout = new QVBoxLayout();

            layout.addWidget(quit);
            layout.addWidget(lcd);
            layout.addWidget(slider);
            setLayout(layout);

            setWindowTitle(tr("Building Blocks"));
        }

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

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

            QApplication.exec();
        }
    }

## Line by Line Walkthrough

        public Blocks()
        {
            QPushButton quit = new QPushButton(tr("Quit"));
            quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));

            QLCDNumber lcd = new QLCDNumber(2);
            lcd.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);

`lcd` is a QLCDNumber, a widget that displays numbers in an LCD-like fashion. This instance is set up to display two digits. We set the QLCDNumber.segmentStyle property to QLCDNumber.SegmentStyle.Filled to make the LCDs more readable.

            QSlider slider = new QSlider(Qt.Orientation.Horizontal);

            slider.setRange(0, 99);

            slider.setValue(0);

The user can use the QSlider widget to adjust an integer value in a range. Here we create a horizontal one, set its minimum value to 0, its maximum value to 99, and its initial value to 0.

            slider.valueChanged.connect(lcd, "display(int)");

Here we use the signals and slots mechanism to connect the slider's valueChanged signal to the LCD number's display() slot.

Whenever the slider's value changes it broadcasts the new value by emitting the valueChanged signal. Because that signal is connected to the LCD number's display() slot, the slot is called when the signal is broadcast. Neither of the objects knows about the other. This is essential in component programming.

Slots are otherwise normal Java member methods and follow the normal Java access rules.

            QVBoxLayout layout = new QVBoxLayout();

            layout.addWidget(quit);
            layout.addWidget(lcd);
            layout.addWidget(slider);
            setLayout(layout);

`Blocks` uses a QVBoxLayout to manage the geometry of its child widgets. For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the `quit`, `lcd` and `slider` widgets to the layout using QBoxLayout.addWidget().

The QWidget.setLayout() method installs the layout on `Blocks`. The call to setLayout() automatically sets the parent of the widgets in the layout so that they are children of `Blocks`. Because of this, we didn't need to specify `this` as the parent for the `quit`, `lcd` and `slider` widgets.

In Qt Jambi, widgets are either children of other widgets (e.g. `this`), or they have no parent. A widget can be added to a layout, in which case the layout becomes responsible for managing the geometry of that widget, but the layout can never act as a parent itself. Indeed, QWidget's constructor takes a QWidget pointer for the parent, and QLayout doesn't inherit from QWidget.

## Running the Application

The LCD number reflects everything you do to the slider, and the widget handles resizing well. Notice that the LCD number widget changes in size when the window is resized (because it can), but the others stay about the same (because otherwise they would look strange).

## Exercises

Try changing the LCD number to add more digits or to change mode (QLCDNumber.setMode()). You can even add four push buttons to set the number base.

You can also change the slider's range.

Perhaps it would have been better to use a QSpinBox than a slider?

Try to make the application quit when the LCD number overflows.