[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:216 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Qt Tutorial - Chapter 2: Calling it Quits</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 2: Calling it Quits</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p> <center><img src="t2.png" alt="Screenshot of tutorial two"></center>
|
---|
| 36 | <p> Having created a window in <a href="tutorial1-01.html">Chapter 1,</a> we will
|
---|
| 37 | now go on to make the application quit properly when the user tells it to.
|
---|
| 38 | <p> We will also use a font that is more exciting than the default one.
|
---|
| 39 | <p> <pre>/****************************************************************
|
---|
| 40 | **
|
---|
| 41 | ** Qt tutorial 2
|
---|
| 42 | **
|
---|
| 43 | ****************************************************************/
|
---|
| 44 |
|
---|
| 45 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 46 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
| 47 | #include <<a href="qfont-h.html">qfont.h</a>>
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | int main( int argc, char **argv )
|
---|
| 51 | {
|
---|
| 52 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 53 |
|
---|
| 54 | <a href="qpushbutton.html">QPushButton</a> quit( "Quit", 0 );
|
---|
| 55 | quit.<a href="qwidget.html#resize">resize</a>( 75, 30 );
|
---|
| 56 | quit.<a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 57 |
|
---|
| 58 | 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>()) );
|
---|
| 59 |
|
---|
| 60 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &quit );
|
---|
| 61 | quit.<a href="qwidget.html#show">show</a>();
|
---|
| 62 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 63 | }
|
---|
| 64 | </pre>
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | <p> <h2> Line-by-line Walkthrough
|
---|
| 69 | </h2>
|
---|
| 70 | <a name="1"></a><p> <pre> #include <<a href="qfont-h.html">qfont.h</a>>
|
---|
| 71 | </pre>
|
---|
| 72 | <p> Since this program uses <a href="qfont.html">QFont</a>, it needs to include qfont.h. Qt's font
|
---|
| 73 | abstraction is rather different from the horror provided by X, and
|
---|
| 74 | loading and using fonts has been highly optimized.
|
---|
| 75 | <p> <pre> <a href="qpushbutton.html">QPushButton</a> quit( "Quit", 0 );
|
---|
| 76 | </pre>
|
---|
| 77 | <p> This time, the button says "Quit" and that's exactly what the program
|
---|
| 78 | will do when the user clicks the button. This is not a coincidence.
|
---|
| 79 | We still pass 0 as the parent, since the button is a top-level window.
|
---|
| 80 | <p> <pre> <a name="x2292"></a> quit.<a href="qwidget.html#resize">resize</a>( 75, 30 );
|
---|
| 81 | </pre>
|
---|
| 82 | <p> We've chosen another size for the button since the text is a bit
|
---|
| 83 | shorter than "Hello world!". We could also have used <a href="qfontmetrics.html">QFontMetrics</a>
|
---|
| 84 | to set right size.
|
---|
| 85 | <p> <pre> <a name="x2293"></a> quit.<a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 86 | </pre>
|
---|
| 87 | <p> Here we choose a new font for the button, an 18-point bold font from
|
---|
| 88 | the Times family. Note that we create the font on the spot.
|
---|
| 89 | <p> It is also possible to change the default font (using <a href="qapplication.html#setFont">QApplication::setFont</a>()) for the whole application.
|
---|
| 90 | <p> <pre> <a name="x2291"></a><a name="x2290"></a><a name="x2288"></a> 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>()) );
|
---|
| 91 | </pre>
|
---|
| 92 | <p> connect() is perhaps <em>the</em> most central feature of Qt.
|
---|
| 93 | Note that connect() is a static function in <a href="qobject.html">QObject</a>. Do not confuse it
|
---|
| 94 | with the connect() function in the socket library.
|
---|
| 95 | <p> This line establishes a one-way connection between two Qt objects (objects
|
---|
| 96 | that inherit QObject, directly or indirectly). Every Qt object can have
|
---|
| 97 | both <tt>signals</tt> (to send messages) and <tt>slots</tt> (to receive messages). All
|
---|
| 98 | widgets are Qt objects. They inherit <a href="qwidget.html">QWidget</a> which in turn inherits
|
---|
| 99 | QObject.
|
---|
| 100 | <p> Here, the <em>clicked()</em> signal of <em>quit</em> is connected to the <em>quit()</em> slot of <em>a</em>, so that when the button is clicked, the
|
---|
| 101 | application quits.
|
---|
| 102 | <p> The <a href="signalsandslots.html">Signals and Slots</a> documentation
|
---|
| 103 | describes this topic in detail.
|
---|
| 104 | <p> <h2> Behavior
|
---|
| 105 | </h2>
|
---|
| 106 | <a name="2"></a><p> When you run this program, you will see an even smaller window than in
|
---|
| 107 | Chapter 1, filled with an even smaller button.
|
---|
| 108 | <p> (See <a href="tutorial1-01.html#compiling">Compiling</a> for how to create a
|
---|
| 109 | makefile and build the application.)
|
---|
| 110 | <p> <h2> Exercises
|
---|
| 111 | </h2>
|
---|
| 112 | <a name="3"></a><p> Try to resize the window. Press the button. Oops! That connect()
|
---|
| 113 | would seem to make some difference.
|
---|
| 114 | <p> Are there any other signals in <a href="qpushbutton.html">QPushButton</a> you can connect to quit?
|
---|
| 115 | Hint: The QPushButton inherits most of its behavior from <a href="qbutton.html">QButton</a>.
|
---|
| 116 | <p> You're now ready for <a href="tutorial1-03.html">Chapter 3.</a>
|
---|
| 117 | <p> [<a href="tutorial1-01.html">Previous tutorial</a>]
|
---|
| 118 | [<a href="tutorial1-03.html">Next tutorial</a>]
|
---|
| 119 | [<a href="tutorial.html">Main tutorial page</a>]
|
---|
| 120 | <p>
|
---|
| 121 | <!-- eof -->
|
---|
| 122 | <p><address><hr><div align=center>
|
---|
| 123 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 124 | <td>Copyright © 2007
|
---|
| 125 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 126 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 127 | </table></div></address></body>
|
---|
| 128 | </html>
|
---|