[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:378 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Qt Tutorial - Chapter 4: Let There Be Widgets</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 4: Let There Be Widgets</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p> <center><img src="t4.png" alt="Screenshot of tutorial four"></center>
|
---|
| 36 | <p> This example shows how to create your own widget, describes how to control the
|
---|
| 37 | minimum and maximum sizes of a widget, and introduces widget names.
|
---|
| 38 | <p> <pre>/****************************************************************
|
---|
| 39 | **
|
---|
| 40 | ** Qt tutorial 4
|
---|
| 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 |
|
---|
| 48 |
|
---|
| 49 | class MyWidget : public <a href="qwidget.html">QWidget</a>
|
---|
| 50 | {
|
---|
| 51 | public:
|
---|
| 52 | MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | <a name="f552"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 57 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
| 58 | {
|
---|
| 59 | <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 200, 120 );
|
---|
| 60 | <a href="qwidget.html#setMaximumSize">setMaximumSize</a>( 200, 120 );
|
---|
| 61 |
|
---|
| 62 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
|
---|
| 63 | quit-><a href="qwidget.html#setGeometry">setGeometry</a>( 62, 40, 75, 30 );
|
---|
| 64 | quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 65 |
|
---|
| 66 | <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>()) );
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | int main( int argc, char **argv )
|
---|
| 71 | {
|
---|
| 72 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 73 |
|
---|
| 74 | MyWidget w;
|
---|
| 75 | w.<a href="qwidget.html#setGeometry">setGeometry</a>( 100, 100, 200, 120 );
|
---|
| 76 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &w );
|
---|
| 77 | w.<a href="qwidget.html#show">show</a>();
|
---|
| 78 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 79 | }
|
---|
| 80 | </pre>
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | <p> <h2> Line-by-line Walkthrough
|
---|
| 85 | </h2>
|
---|
| 86 | <a name="1"></a><p> <pre> class MyWidget : public <a href="qwidget.html">QWidget</a>
|
---|
| 87 | {
|
---|
| 88 | public:
|
---|
| 89 | MyWidget( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
|
---|
| 90 | };
|
---|
| 91 | </pre>
|
---|
| 92 | <p> Here we create a new class. Because this class inherits from <a href="qwidget.html">QWidget</a>,
|
---|
| 93 | the new class is a widget and may be a top level window or a child
|
---|
| 94 | widget (like the push button in Chapter 3).
|
---|
| 95 | <p> This class has only one member, a constructor (in addition to the
|
---|
| 96 | members it inherits from QWidget). The constructor is a standard Qt
|
---|
| 97 | widget constructor; you should always include a similar constructor
|
---|
| 98 | when you create widgets.
|
---|
| 99 | <p> The first argument is its parent widget. To create a top-level window
|
---|
| 100 | you specify a null pointer as the parent. As you can see, this widget
|
---|
| 101 | defaults to be a top-level window.
|
---|
| 102 | <p> The second argument is the widget's name. This is <em>not</em> the text
|
---|
| 103 | that appears in the window's title bar or in the button. It is a name
|
---|
| 104 | associated with a widget to make it possible to <a href="qobject.html#queryList">look up</a> this widget later, and there is
|
---|
| 105 | also a <a href="qobject.html#dumpObjectTree">handy debugging function</a> that will list a complete widget hierarchy.
|
---|
| 106 | <p> <pre> MyWidget::MyWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 107 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
| 108 | </pre>
|
---|
| 109 | <p> The implementation of the constructor starts here. Like most widgets,
|
---|
| 110 | it just passes on the <tt>parent</tt> and <tt>name</tt> to the <a href="qwidget.html">QWidget</a>
|
---|
| 111 | constructor.
|
---|
| 112 | <p> <pre> {
|
---|
| 113 | <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 200, 120 );
|
---|
| 114 | <a href="qwidget.html#setMaximumSize">setMaximumSize</a>( 200, 120 );
|
---|
| 115 | </pre>
|
---|
| 116 | <p> Because this widget doesn't know how to handle resizing, we fix its size
|
---|
| 117 | by setting the minimum and maximum to be equal. In the next chapter
|
---|
| 118 | we will show how a widget can respond to resize event from the user.
|
---|
| 119 | <p> <pre> <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
|
---|
| 120 | <a name="x2308"></a> quit-><a href="qwidget.html#setGeometry">setGeometry</a>( 62, 40, 75, 30 );
|
---|
| 121 | <a name="x2307"></a> quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 122 | </pre>
|
---|
| 123 | <p> Here we create and set up a child widget of this widget (the new widget's
|
---|
| 124 | parent is <tt>this</tt>) which has the widget name "quit". The widget
|
---|
| 125 | name has nothing to do with the button text; it just happens to be
|
---|
| 126 | similar in this case.
|
---|
| 127 | <p> Note that <tt>quit</tt> is a local variable in the constructor. MyWidget
|
---|
| 128 | does not keep track of it, but Qt does, and will by default delete it
|
---|
| 129 | when MyWidget is deleted. This is why MyWidget doesn't need a
|
---|
| 130 | destructor. (On the other hand, there is no harm in deleting a child
|
---|
| 131 | when you choose to, the child will automatically tell Qt about its
|
---|
| 132 | imminent death.)
|
---|
| 133 | <p> The setGeometry() call does the same as move() and resize() did in the
|
---|
| 134 | previous chapters.
|
---|
| 135 | <p> <pre> <a name="x2306"></a><a name="x2304"></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>()) );
|
---|
| 136 | }
|
---|
| 137 | </pre>
|
---|
| 138 | <p> Because the MyWidget class doesn't know about the application object, it
|
---|
| 139 | has to connect to Qt's pointer to it, <tt>qApp</tt>.
|
---|
| 140 | <p> A widget is a software component and should know as little as possible
|
---|
| 141 | about its environment in order to be as general and reusable as
|
---|
| 142 | possible.
|
---|
| 143 | <p> Knowing the name of the application object would break this principle,
|
---|
| 144 | so Qt offers an alias, qApp, for the cases in which a component such as
|
---|
| 145 | MyWidget needs to talk to the application object.
|
---|
| 146 | <p> <pre> int main( int argc, char **argv )
|
---|
| 147 | {
|
---|
| 148 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 149 |
|
---|
| 150 | MyWidget w;
|
---|
| 151 | w.<a href="qwidget.html#setGeometry">setGeometry</a>( 100, 100, 200, 120 );
|
---|
| 152 | <a name="x2305"></a> a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &w );
|
---|
| 153 | <a name="x2309"></a> w.<a href="qwidget.html#show">show</a>();
|
---|
| 154 | <a name="x2303"></a> return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 155 | }
|
---|
| 156 | </pre>
|
---|
| 157 | <p> Here we instantiate our new child, set it to be the main widget, and
|
---|
| 158 | execute the application.
|
---|
| 159 | <p> <h2> Behavior
|
---|
| 160 | </h2>
|
---|
| 161 | <a name="2"></a><p> This program is very similar in behavior to the previous one. The
|
---|
| 162 | difference lies in the way we have implemented it. It does behave
|
---|
| 163 | slightly differently, however. Just try to resize it to see.
|
---|
| 164 | <p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
|
---|
| 165 | makefile and build the application.)
|
---|
| 166 | <p> <h2> Exercises
|
---|
| 167 | </h2>
|
---|
| 168 | <a name="3"></a><p> Try to create another MyWidget object in main(). What happens?
|
---|
| 169 | <p> Try to add more buttons or put in widgets other than <a href="qpushbutton.html">QPushButton</a>.
|
---|
| 170 | <p> You're now ready for <a href="tutorial1-05.html">Chapter 5.</a>
|
---|
| 171 | <p> [<a href="tutorial1-03.html">Previous tutorial</a>]
|
---|
| 172 | [<a href="tutorial1-05.html">Next tutorial</a>]
|
---|
| 173 | [<a href="tutorial.html">Main tutorial page</a>]
|
---|
| 174 | <p>
|
---|
| 175 | <!-- eof -->
|
---|
| 176 | <p><address><hr><div align=center>
|
---|
| 177 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 178 | <td>Copyright © 2007
|
---|
| 179 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 180 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 181 | </table></div></address></body>
|
---|
| 182 | </html>
|
---|