1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Tutorial - Chapter 5: Building Blocks
</title></head><body bgcolor="#ffffff">
<p>
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<p>
<h1 align=center>Chapter 5: Building Blocks</h1><br clear="all">
<p>
<center><img src="t5.png" alt="Screenshot of tutorial five"></center>
<p>
This example shows how to create and connect together several widgets
using signals and slots, and how to handle resize events. <pre>/****************************************************************
**
** Qt tutorial 5
**
****************************************************************/
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
#include <<a href="qslider-h.html">qslider.h</a>>
#include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
#include <<a href="qfont-h.html">qfont.h</a>>
#include <<a href="qvbox-h.html">qvbox.h</a>>
class MyWidget : public QVBox
{
public:
MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
};
MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qvbox.html">QVBox</a>( parent, name )
{
<a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
quit-><a href="qwidget.html#dde00b">setFont</a>( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );
<a href="qobject.html#c2d209">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
<a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
<a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
slider-><a href="qrangecontrol.html#0337f8">setRange</a>( 0, 99 );
slider-><a href="qslider.html#c5cce4">setValue</a>( 0 );
<a href="qobject.html#c2d209">connect</a>( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
}
int main( int argc, char **argv )
{
<a href="qapplication.html">QApplication</a> a( argc, argv );
MyWidget w;
a.<a href="qapplication.html#11c921">setMainWidget</a>( &w );
w.<a href="qwidget.html#ccc8f7">show</a>();
return a.<a href="qapplication.html#7076af">exec</a>();
}
</pre>
<p>
<h2>Line by Line Walk-Through</h2> <pre>
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
#include <<a href="qslider-h.html">qslider.h</a>>
#include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
#include <<a href="qfont-h.html">qfont.h</a>>
#include <<a href="qvbox-h.html">qvbox.h</a>>
</pre>
<p>
Three new include files here. qslider.h and qlcdnumber.h are there
because we use two new widgets; QSlider and QLCDNumber. qvbox.h is
there because we use Qt's automatic layout support. <pre>
class MyWidget : public QVBox
{
public:
MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
};
</pre>
<p>
<a name=constructor></a> <pre>
MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qvbox.html">QVBox</a>( parent, name )
{
</pre>
<p>
MyWidget is now derived from QVBox instead of QWidget. That way we use
the layout of the QVBox (which places all of its children vertically
inside itself). Resizes are handled automatically by the QVBox and
therefore by MyWidget too, now. <pre>
<a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
</pre>
<p>
<code>lcd</code> is a QLCDNumber, a widget which displays numbers in an LCD-like
fashion. This instance is set up to display two digits, be a child of
<em>this</em> and is named "lcd". <pre>
<a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
slider-><a href="qrangecontrol.html#0337f8">setRange</a>( 0, 99 );
slider-><a href="qslider.html#c5cce4">setValue</a>( 0 );
</pre>
<p>
QSlider is a classical slider; a widget where the user can drag
something to adjust an integer value in a range. Here, we create a
horizontal one, set its range to 0-99 (inclusive, see the <a href="qrangecontrol.html#0337f8">QSlider::setRange()</a> documentation) and its initial value to 0. <pre>
<a href="qobject.html#c2d209">connect</a>( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
</pre>
<p>
Here we use the <a href="signalsandslots.html">signal/slot mechanism</a>
to connect the slider's valueChanged() signal to the LCD number's
display() slot.
<p>
Whenever the slider's value changes, it broadcasts the new value by
emitting the valueChanged() signal. Since that signal is connected to
the LCD number's display() slot, the slot is called when the signal is
broadcast. Neither of the objects know about the other. This is
essential in component programming.
<p>
Slots are otherwise normal C++ member functions and follow the normal
C++ access rules.
<p>
<h2>Behavior</h2>
<p>
The LCD number reflects everything you do to the slider, and the
widget handles resizing well. Notice how the LCD number widget
changes in size when the window is resized (because it can), but the
others stay about the same (because they would look stupid otherwise).
<p>
<h2>Exercises</h2>
<p>
Try changing the LCD number to add more digits or <a href="qlcdnumber.html#b7f1d1">change mode.</a> You can even add four push
buttons to set the number base.
<p>
You can also change the slider's range.
<p>
Perhaps it would have been better to use <a href="qspinbox.html">QSpinBox</a> than a slider?
<p>
Try to make the application quit when the LCD number overflows.
<p>
You may now go on to <a href="t6.html">chapter six.</a>
<p>
[<a href="t4.html">Previous tutorial</a>]
[<a href="t6.html">Next tutorial</a>]
[<a href="tutorial.html">Main tutorial page</a>]
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>
|