| 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:303 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>Qt Tutorial - Chapter 3: Family Values</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 3: Family Values</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | <p> <center><img src="t3.png" alt="Screenshot of tutorial three"></center> | 
|---|
| 36 | <p> This example shows how to create parent and child widgets. | 
|---|
| 37 | <p> We'll keep it simple and use just a single parent and a lone child. | 
|---|
| 38 | <p> <pre>/**************************************************************** | 
|---|
| 39 | ** | 
|---|
| 40 | ** Qt tutorial 3 | 
|---|
| 41 | ** | 
|---|
| 42 | ****************************************************************/ | 
|---|
| 43 |  | 
|---|
| 44 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 45 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>> | 
|---|
| 46 | #include <<a href="qfont-h.html">qfont.h</a>> | 
|---|
| 47 | #include <<a href="qvbox-h.html">qvbox.h</a>> | 
|---|
| 48 |  | 
|---|
| 49 | int main( int argc, char **argv ) | 
|---|
| 50 | { | 
|---|
| 51 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 52 |  | 
|---|
| 53 | <a href="qvbox.html">QVBox</a> box; | 
|---|
| 54 | box.<a href="qwidget.html#resize">resize</a>( 200, 120 ); | 
|---|
| 55 |  | 
|---|
| 56 | <a href="qpushbutton.html">QPushButton</a> quit( "Quit", &box ); | 
|---|
| 57 | quit.<a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) ); | 
|---|
| 58 |  | 
|---|
| 59 | QObject::<a href="qobject.html#connect">connect</a>( &quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), &a, SLOT(<a href="qapplication.html#quit">quit</a>()) ); | 
|---|
| 60 |  | 
|---|
| 61 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &box ); | 
|---|
| 62 | box.<a href="qwidget.html#show">show</a>(); | 
|---|
| 63 |  | 
|---|
| 64 | return a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 65 | } | 
|---|
| 66 | </pre> | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | <p> <h2> Line-by-line Walkthrough | 
|---|
| 71 | </h2> | 
|---|
| 72 | <a name="1"></a><p> <pre>    #include <<a href="qvbox-h.html">qvbox.h</a>> | 
|---|
| 73 | </pre> | 
|---|
| 74 | <p> We add an include of qvbox.h to get the layout class we'll use. | 
|---|
| 75 | <p> <pre>        <a href="qvbox.html">QVBox</a> box; | 
|---|
| 76 | </pre> | 
|---|
| 77 | <p> Here we simply create a vertical box container. The <a href="qvbox.html">QVBox</a> arranges | 
|---|
| 78 | its child widgets in a vertical row, one above the other, handing out | 
|---|
| 79 | space according to each child's <a href="qwidget.html#sizePolicy">QWidget::sizePolicy</a>(). | 
|---|
| 80 | <p> <pre>    <a name="x2300"></a>    box.<a href="qwidget.html#resize">resize</a>( 200, 120 ); | 
|---|
| 81 | </pre> | 
|---|
| 82 | <p> We set its width to 200 pixels and the height to 120 pixels. | 
|---|
| 83 | <p> <pre>        <a href="qpushbutton.html">QPushButton</a> quit( "Quit", &box ); | 
|---|
| 84 | </pre> | 
|---|
| 85 | <p> A child is born. | 
|---|
| 86 | <p> This <a href="qpushbutton.html">QPushButton</a> is created with both a text ("Quit") and a parent | 
|---|
| 87 | (box). A child widget is always on top of its parent. When | 
|---|
| 88 | displayed, it is clipped by its parent's bounds. | 
|---|
| 89 | <p> The parent widget, the QVBox, automatically adds the child centered in | 
|---|
| 90 | its box. Because nothing else is added, the button gets all the space | 
|---|
| 91 | the parent has. | 
|---|
| 92 | <p> <pre>    <a name="x2302"></a>    box.<a href="qwidget.html#show">show</a>(); | 
|---|
| 93 | </pre> | 
|---|
| 94 | <p> When a parent widget is shown, it will call show for all its children | 
|---|
| 95 | (except those on which you have done an explicit <a href="qwidget.html#hide">QWidget::hide</a>()). | 
|---|
| 96 | <p> <h2> Behavior | 
|---|
| 97 | </h2> | 
|---|
| 98 | <a name="2"></a><p> The button no longer fills the entire widget. Instead, it gets a | 
|---|
| 99 | "natural" size. This is because there is now a new top-level widget, | 
|---|
| 100 | which uses the button's size hint and size change policy to set the | 
|---|
| 101 | button's size and position. (See <a href="qwidget.html#sizeHint">QWidget::sizeHint</a>() and <a href="qwidget.html#setSizePolicy">QWidget::setSizePolicy</a>() for more information about these functions.) | 
|---|
| 102 | <p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a | 
|---|
| 103 | makefile and build the application.) | 
|---|
| 104 | <p> <h2> Exercises | 
|---|
| 105 | </h2> | 
|---|
| 106 | <a name="3"></a><p> Try resizing the window. How does the button change?  What is the | 
|---|
| 107 | button's size-change policy?  What happens to the button's height if | 
|---|
| 108 | you run the program with a bigger font?  What happens if you try to | 
|---|
| 109 | make the window <em>really</em> small? | 
|---|
| 110 | <p> You're now ready for <a href="tutorial1-04.html">Chapter 4.</a> | 
|---|
| 111 | <p> [<a href="tutorial1-02.html">Previous tutorial</a>] | 
|---|
| 112 | [<a href="tutorial1-04.html">Next tutorial</a>] | 
|---|
| 113 | [<a href="tutorial.html">Main tutorial page</a>] | 
|---|
| 114 | <p> | 
|---|
| 115 | <!-- eof --> | 
|---|
| 116 | <p><address><hr><div align=center> | 
|---|
| 117 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 118 | <td>Copyright © 2007 | 
|---|
| 119 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 120 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 121 | </table></div></address></body> | 
|---|
| 122 | </html> | 
|---|