[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:580 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Qt Tutorial - Chapter 6: Building Blocks Galore!</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Qt Tutorial - Chapter 6: Building Blocks Galore!</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p> <center><img src="t6.png" alt="Screenshot of tutorial six"></center>
|
---|
| 36 | <p> This example shows how to encapsulate two widgets into a new component and
|
---|
| 37 | how easy it is to use many widgets. For the first time, we use a custom
|
---|
| 38 | widget as a child widget.
|
---|
| 39 | <p> <a name="main"></a>
|
---|
| 40 | <pre>/****************************************************************
|
---|
| 41 | **
|
---|
| 42 | ** Qt tutorial 6
|
---|
| 43 | **
|
---|
| 44 | ****************************************************************/
|
---|
| 45 |
|
---|
| 46 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 47 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
| 48 | #include <<a href="qslider-h.html">qslider.h</a>>
|
---|
| 49 | #include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
|
---|
| 50 | #include <<a href="qfont-h.html">qfont.h</a>>
|
---|
| 51 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
| 52 | #include <<a href="qgrid-h.html">qgrid.h</a>>
|
---|
| 53 |
|
---|
| 54 | class LCDRange : public <a href="qvbox.html">QVBox</a>
|
---|
| 55 | {
|
---|
| 56 | public:
|
---|
| 57 | LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | <a name="f554"></a>LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 61 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
| 62 | {
|
---|
| 63 | <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
|
---|
| 64 | <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
|
---|
| 65 | slider-><a href="qrangecontrol.html#setRange">setRange</a>( 0, 99 );
|
---|
| 66 | slider-><a href="qslider.html#setValue">setValue</a>( 0 );
|
---|
| 67 | <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | class MyWidget : public <a href="qvbox.html">QVBox</a>
|
---|
| 71 | {
|
---|
| 72 | public:
|
---|
| 73 | MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | <a name="f555"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 78 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
| 79 | {
|
---|
| 80 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
|
---|
| 81 | quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 82 |
|
---|
| 83 | <a href="qobject.html#connect">connect</a>( quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 84 |
|
---|
| 85 | <a href="qgrid.html">QGrid</a> *grid = new <a href="qgrid.html">QGrid</a>( 4, this );
|
---|
| 86 |
|
---|
| 87 | for( int r = 0 ; r < 4 ; r++ )
|
---|
| 88 | for( int c = 0 ; c < 4 ; c++ )
|
---|
| 89 | (void)new LCDRange( grid );
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | int main( int argc, char **argv )
|
---|
| 93 | {
|
---|
| 94 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 95 |
|
---|
| 96 | MyWidget w;
|
---|
| 97 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &w );
|
---|
| 98 | w.<a href="qwidget.html#show">show</a>();
|
---|
| 99 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 100 | }
|
---|
| 101 | </pre>
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | <p> <h2> Line-by-line Walkthrough
|
---|
| 106 | </h2>
|
---|
| 107 | <a name="1"></a><p> <pre> class LCDRange : public <a href="qvbox.html">QVBox</a>
|
---|
| 108 | {
|
---|
| 109 | public:
|
---|
| 110 | LCDRange( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
| 111 | };
|
---|
| 112 | </pre>
|
---|
| 113 | <p> The LCDRange widget is a widget without any API. It just has a
|
---|
| 114 | constructor. This sort of widget is not very useful, so we'll add some API later.
|
---|
| 115 | <p> <pre> LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 116 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
| 117 | {
|
---|
| 118 | <a href="qlcdnumber.html">QLCDNumber</a> *lcd = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "lcd" );
|
---|
| 119 | <a href="qslider.html">QSlider</a> * slider = new <a href="qslider.html">QSlider</a>( Horizontal, this, "slider" );
|
---|
| 120 | <a name="x2325"></a> slider-><a href="qrangecontrol.html#setRange">setRange</a>( 0, 99 );
|
---|
| 121 | <a name="x2326"></a> slider-><a href="qslider.html#setValue">setValue</a>( 0 );
|
---|
| 122 | <a name="x2327"></a><a name="x2324"></a> <a href="qobject.html#connect">connect</a>( slider, SIGNAL(<a href="qslider.html#valueChanged">valueChanged</a>(int)), lcd, SLOT(<a href="qlcdnumber.html#display">display</a>(int)) );
|
---|
| 123 | }
|
---|
| 124 | </pre>
|
---|
| 125 | <p> This is lifted straight from the
|
---|
| 126 | <a href="tutorial1-05.html#constructor">MyWidget constructor</a> in Chapter 5.
|
---|
| 127 | The only differences are that the button is left out and the class
|
---|
| 128 | is renamed.
|
---|
| 129 | <p> <pre> class MyWidget : public <a href="qvbox.html">QVBox</a>
|
---|
| 130 | {
|
---|
| 131 | public:
|
---|
| 132 | MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
| 133 | };
|
---|
| 134 | </pre>
|
---|
| 135 | <p> MyWidget, too, contains no API except a constructor.
|
---|
| 136 | <p> <pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 137 | : <a href="qvbox.html">QVBox</a>( parent, name )
|
---|
| 138 | {
|
---|
| 139 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
|
---|
| 140 | <a name="x2328"></a> quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 141 |
|
---|
| 142 | <a name="x2321"></a> <a href="qobject.html#connect">connect</a>( quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 143 | </pre>
|
---|
| 144 | <p> The push button that used to be in what is now LCDRange has been
|
---|
| 145 | separated so that we can have one "Quit" button and many LCDRange
|
---|
| 146 | objects.
|
---|
| 147 | <p> <pre> <a href="qgrid.html">QGrid</a> *grid = new <a href="qgrid.html">QGrid</a>( 4, this );
|
---|
| 148 | </pre>
|
---|
| 149 | <p> We create a <a href="qgrid.html">QGrid</a> object with four columns. The QGRid widget
|
---|
| 150 | automatically arranges its children in rows and columns; you can
|
---|
| 151 | specify the number of rows or of columns, and QGrid will discover its
|
---|
| 152 | new children and fit them into the grid.
|
---|
| 153 | <p> <pre> for( int r = 0 ; r < 4 ; r++ )
|
---|
| 154 | for( int c = 0 ; c < 4 ; c++ )
|
---|
| 155 | (void)new LCDRange( grid );
|
---|
| 156 | </pre>
|
---|
| 157 | <p> Four columns, four rows.
|
---|
| 158 | <p> We create 4*4 LCDRanges, all of which are children of the grid object.
|
---|
| 159 | The QGrid widget will arrange them.
|
---|
| 160 | <p> <pre> }
|
---|
| 161 | </pre>
|
---|
| 162 | <p> That's all.
|
---|
| 163 | <p> <h2> Behavior
|
---|
| 164 | </h2>
|
---|
| 165 | <a name="2"></a><p> This program shows how easy it is to use many widgets at a time. Each
|
---|
| 166 | one behaves like the slider and LCD number in the previous
|
---|
| 167 | chapter. Again, the difference lies in the implementation.
|
---|
| 168 | <p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
|
---|
| 169 | makefile and build the application.)
|
---|
| 170 | <p> <h2> Exercises
|
---|
| 171 | </h2>
|
---|
| 172 | <a name="3"></a><p> Initialize each slider with a different/random value on startup.
|
---|
| 173 | <p> The source contains three occurrences of "4". What happens if you
|
---|
| 174 | change the one in the <a href="qgrid.html">QGrid</a> constructor call? What about the other
|
---|
| 175 | two? Why is this?
|
---|
| 176 | <p> You're now ready for <a href="tutorial1-07.html">Chapter 7.</a>
|
---|
| 177 | <p> [<a href="tutorial1-05.html">Previous tutorial</a>]
|
---|
| 178 | [<a href="tutorial1-07.html">Next tutorial</a>]
|
---|
| 179 | [<a href="tutorial.html">Main tutorial page</a>]
|
---|
| 180 | <p>
|
---|
| 181 | <!-- eof -->
|
---|
| 182 | <p><address><hr><div align=center>
|
---|
| 183 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 184 | <td>Copyright © 2007
|
---|
| 185 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 186 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 187 | </table></div></address></body>
|
---|
| 188 | </html>
|
---|