Qt logo


Chapter 7: One Thing Leads to Another


Screenshot of tutorial seven

This example shows how to create custom widgets with signals and slots, and how to connect them together in more complex ways. For the first time, the source is split among several files.

Line by Line Walk-Through

lcdrange.h

This file is mainly lifted from main.cpp in chapter 6 and only the changes are noted here.

    #ifndef LCDRANGE_H
    #define LCDRANGE_H

This is the classical C construction to avoid errors if a header file happens to be included more than once. If you don't use it already: It is a very good habit. The #ifndef should enclose all of the header file.

    #include <qwidget.h>
    class QScrollBar;
    class QLCDNumber;

qwidget.h is included. LCDRange inherits QWidget, and the header file of a parent class must always be included. Until now, qwidget.h has been included indirectly via other header files like qpushbutton.h.

Since the class declaration only uses pointers to QScrollBar and QLCDNumber it does not need their definitions, so we merely declare their names in the header files. This makes the job a little easier for the compiler.

    class LCDRange : public QWidget
    {
        Q_OBJECT
    public:
        LCDRange( QWidget *parent=0, const char *name=0 );

Note the Q_OBJECT. This macro must be included in all classes that contain signals and/or slots. For the curious, it defines the functions that are implemented in the meta object file.

        int value() const;
    public slots:
        void setValue( int );
    signals:
        void valueChanged( int );

These three members make up an interface between this widget and other components in a program. Until now, LCDRange didn't really have an interface at all.

value() is a public function for accessing the value of the LCDRange. setValue() is our first custom slot and valueChanged() is our first custom signal.

Slots must be implemented in the normal way (remember, a slot is also a C++ member function). Signals are automatically implemented in the meta object file. Signals follow the access rules of protected C++ functions, i.e. they can only be emitted by the class they are defined in or by classes inheriting from it.

lcdrange.cpp

This file is mainly lifted from t6/main.cpp and only the changes are noted here.

        connect( sBar, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
        connect( sBar, SIGNAL(valueChanged(int)), SIGNAL(valueChanged(int)) );

This code is from the LCDRange constructor.

The first connect is the same you've seen in the previous chapter. The second is new: It connects sBar's valueChanged() signal to this object's valueChanged signal. connect() with 3 arguments always connect to signals or slots in this object.

Yes, that's right. Signals can be connected to other signals. When the first is emitted, the second signal is also emitted.

Let's look at what happens when the user operates the scroll bar: The scroll bar sees that its value has changed, and emits the valueChanged() signal. That signal is connected both to the display() slot of the QLCDNumber and to the valueChanged() signal of the LCDRange.

Thus, when the signal is emitted, LCDRange emits its own valueChanged() signal. In addition, QLCDNumber::display() is called and shows the new number.

Note that you're not guaranteed any particular order of execution.

    int LCDRange::value() const
    {
        return sBar->value();
    }

The implementation of value() is straight forward, it simply returns the scroll bar's value.

    void LCDRange::setValue( int value )
    {
        sBar->setValue( value );
    }

The implementation of setValue() is equally straight forward. Note that since the scroll bar and LCD number is connected, setting the scroll bar's value automatically updates the LCD number as well. In addition, the scroll bar will automatically adjust the value if it is outside the scroll bar's legal range.

main.cpp

        for( int i = 0 ; i < 16 ; i++ ) {
            value[i] = new LCDRange( this );
            if ( i > 0 )
                connect( value[i], SIGNAL(valueChanged(int)), 
                         value[i - 1], SLOT(setValue(int)) );
        }

Apart from moving LCDRange into two separate files, these lines are the only changes to the main.cpp file. As in the previous chapter, we create 16 LCDRange objects. In addition, we now connect them together using the signal/slot mechanism. Each one has its valueChanged() signal connected to the setValue() slot in the previous one. Since LCDRange emits the signal valueChanged() when its value changes (surprise!), we are here creating a "chain" of signals and slots.

Behavior

On startup, the program's appearance is identical to the previous one. Try operating the scroll bar to the bottom right...

Excercises

Use the bottom right scroll bar to set all LCDs to 30. Then set the top half to 29 by using the rightmost scroll bar on the 2nd row. Now, use the one to the left of the last one operated to set the first seven LCDs back to 30. Click on the left arrow on the bottom right scroll bar. What happens? Why is this the correct behavior?

You may now go on to chapter eight.

[Previous tutorial] [Next tutorial] [Main tutorial page]


Copyright © 1998 Troll TechTrademarks
Qt version 1.42