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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
<!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 6: Building Blocks Galore!
</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 6: Building Blocks Galore!</h1><br clear="all">
<p>
<center><img src="t6.png" alt="Screenshot of tutorial six"></center>
<p>
This example shows how to encapsulate two widgets into a new component and
how easy it is to use many widgets. For the first time, we use a custom
widget as a child widget.
<p>
<a name=main></a> <pre>/****************************************************************
**
** Qt tutorial 6
**
****************************************************************/
#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>>
#include <<a href="qgrid-h.html">qgrid.h</a>>
class LCDRange : public QVBox
{
public:
LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
};
LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qvbox.html">QVBox</a>( parent, name )
{
<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#f2409c">setRange</a>( 0, 99 );
slider-><a href="qslider.html#a03234">setValue</a>( 0 );
<a href="qobject.html#7f8e37">connect</a>( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
}
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#c52788">setFont</a>( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );
<a href="qobject.html#7f8e37">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
<a href="qgrid.html">QGrid</a> *grid = new <a href="qgrid.html">QGrid</a>( 4, this );
for( int c = 0 ; c < 4 ; c++ )
for( int r = 0 ; r < 4 ; r++ )
(void)new LCDRange( grid );
}
int main( int argc, char **argv )
{
<a href="qapplication.html">QApplication</a> a( argc, argv );
MyWidget w;
a.<a href="qapplication.html#7ad759">setMainWidget</a>( &w );
w.<a href="qwidget.html#200ee5">show</a>();
return a.<a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p>
<h2>Line by Line Walk-Through</h2> <pre>
class LCDRange : public QVBox
{
public:
LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
};
</pre>
<p>
The LCDRange widget is a widget without any API. It just has a
constructor. This sort of widget is not very useful, so later on
we'll add some API. <pre>
LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qvbox.html">QVBox</a>( parent, name )
{
<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#f2409c">setRange</a>( 0, 99 );
slider-><a href="qslider.html#a03234">setValue</a>( 0 );
<a href="qobject.html#7f8e37">connect</a>( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
}
</pre>
<p>
This is lifted straight from the <a
href="t5.html#constructor">MyWidget constructor</a> in chapter five.
The only differences are that the button is left out and the class
renamed. <pre>
class MyWidget : public QVBox
{
public:
MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
};
</pre>
<p>
MyWidget, too, contains no API except a constructor. <pre>
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#c52788">setFont</a>( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );
<a href="qobject.html#7f8e37">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
</pre>
<p>
The push button that used to be in what's now LCDRange has been
separated, so that we can have one "Quit" button and many LCDRange
objects. <pre>
<a href="qgrid.html">QGrid</a> *grid = new <a href="qgrid.html">QGrid</a>( 4, this );
</pre>
<p>
We create a QGrid object with four columns. The QGRid widget
automatically arranges its children in rows and columns; you can
specify the number of rows or of columns, and QGrid will discover its
new children and fit them into the grid. <pre>
for( int c = 0 ; c < 4 ; c++ )
for( int r = 0 ; r < 4 ; r++ )
(void)new LCDRange( grid );
</pre>
<p>
Four columns, for rows.
<p>
We create 4*4 LCDRanges, all of which children of the grid object.
The QGrid widget will arrange them. <pre>
}
</pre>
<p>
That's all.
<p>
<h2>Behavior</h2>
<p>
This program shows how easy it is to use many widgets at a time. Each
single one behaves like the slider and LCD number in the previous
chapter. Again, the difference lies in the implementation.
<p>
<h2>Exercises</h2>
<p>
Initialize each slider with a different/random value on startup.
<p>
The source contains three occurrences of "4". What happens if you
change the one in the <a href="qgrid.html">QGrid</a> constructor call? What about the other
two? Why is this?
<p>
You may now go on to <a href="t7.html">chapter seven.</a>
<p>
[<a href="t5.html">Previous tutorial</a>]
[<a href="t7.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 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|