Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions

[Previous: Chapter 4] [Qt Tutorial] [Next: Chapter 6]

Qt Tutorial 5 - Building Blocks

Files:

Screenshot of tutorial five

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

    /****************************************************************
    **
    ** Qt tutorial 5
    **
    ****************************************************************/

    #include <QApplication>
    #include <QFont>
    #include <QLCDNumber>
    #include <QPushButton>
    #include <QSlider>
    #include <QVBoxWidget>

    class MyWidget : public QVBoxWidget
    {
    public:
        MyWidget(QWidget *parent = 0);
    };

    MyWidget::MyWidget(QWidget *parent)
        : QVBoxWidget(parent)
    {
        QPushButton *quit = new QPushButton("Quit", this);
        quit->setFont(QFont("Times", 18, QFont::Bold));

        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

        QLCDNumber *lcd = new QLCDNumber(2, this);

        QSlider *slider = new QSlider(Qt::Horizontal, this);
        slider->setRange(0, 99);
        slider->setValue(0);

        connect(slider, SIGNAL(valueChanged(int)),
                lcd, SLOT(display(int)));
    }

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MyWidget widget;
        widget.show();
        return app.exec();
    }

Line by Line Walkthrough

    class MyWidget : public QVBoxWidget
    {
    public:
        MyWidget(QWidget *parent = 0);
    };
    MyWidget::MyWidget(QWidget *parent)
        : QVBoxWidget(parent)
    {

MyWidget now derives from QVBoxWidget instead of QWidget. That way we use the QVBoxWidget's layout capabilities (which places all of its children vertically inside itself).

        QLCDNumber *lcd = new QLCDNumber(2, this);

lcd is a QLCDNumber, a widget that displays numbers in an LCD-like fashion. This instance is set up to display two digits and to be a child of this.

        QSlider *slider = new QSlider(Qt::Horizontal, this);
        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.

        connect(slider, SIGNAL(valueChanged(int)),
                lcd, SLOT(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 C++ member functions and follow the normal C++ access rules.

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 stupid).

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 QSpinBox than a slider?

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

[Previous: Chapter 4] [Qt Tutorial] [Next: Chapter 6]


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0-b2