~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t2.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/tutorial.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Qt Tutorial 2 - Calling it Quits</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
    <link rel="prev" href="tutorial-t1.html" />
 
15
    <link rel="contents" href="tutorial.html" />
 
16
    <link rel="next" href="tutorial-t3.html" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
22
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
24
[Previous: <a href="tutorial-t1.html">Chapter 1</a>]
 
25
[<a href="tutorial.html">Qt Tutorial</a>]
 
26
[Next: <a href="tutorial-t3.html">Chapter 3</a>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 2 - Calling it Quits</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
31
<li><a href="tutorial-t2-main-cpp.html">tutorial/t2/main.cpp</a></li>
 
32
</ul>
 
33
<center><img src="images/t2.png" alt="Screenshot of tutorial two" /></center><p>Having created a window in <a href="tutorial-t1.html">Chapter 1,</a> we will now go on to make the application quit properly when the user tells it to.</p>
 
34
<p>We will also use a font that is more exciting than the default one.</p>
 
35
<pre>&nbsp;   /****************************************************************
 
36
    **
 
37
    ** Qt tutorial 2
 
38
    **
 
39
    ****************************************************************/
 
40
 
 
41
    #include &lt;QApplication&gt;
 
42
    #include &lt;QFont&gt;
 
43
    #include &lt;QPushButton&gt;
 
44
 
 
45
    int main(int argc, char *argv[])
 
46
    {
 
47
        QApplication app(argc, argv);
 
48
 
 
49
        QPushButton quit(&quot;Quit&quot;);
 
50
        quit.resize(75, 30);
 
51
        quit.setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));
 
52
 
 
53
        QObject::connect(&amp;quit, SIGNAL(clicked()), &amp;app, SLOT(quit()));
 
54
 
 
55
        quit.show();
 
56
        return app.exec();
 
57
    }</pre>
 
58
<a name="line-by-line-walkthrough"></a>
 
59
<h2>Line by Line Walkthrough</h2>
 
60
<pre>&nbsp;   #include &lt;QFont&gt;</pre>
 
61
<p>Since this program uses <a href="qfont.html">QFont</a>, it needs to include <tt>&lt;QFont&gt;</tt>.</p>
 
62
<pre>&nbsp;       QPushButton quit(&quot;Quit&quot;);</pre>
 
63
<p>This time, the button says <b>Quit</b> and that's exactly what the program will do when the user clicks the button.</p>
 
64
<pre>&nbsp;       quit.resize(75, 30);</pre>
 
65
<p>We've chosen another size for the button since the text is a bit shorter than &quot;Hello world!&quot;. We could also have used <a href="qfontmetrics.html">QFontMetrics</a> to set right size, or let <a href="qpushbutton.html">QPushButton</a> choose a reasonable default.</p>
 
66
<pre>&nbsp;       quit.setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));</pre>
 
67
<p>Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using <a href="qapplication.html#setFont">QApplication::setFont</a>().</p>
 
68
<pre>&nbsp;       QObject::connect(&amp;quit, SIGNAL(clicked()), &amp;app, SLOT(quit()));</pre>
 
69
<p><a href="qobject.html#connect">QObject::connect</a>() is perhaps <i>the</i> most central feature of Qt. Note that <a href="qobject.html#connect">connect()</a> is a static function in <a href="qobject.html">QObject</a>. Do not confuse it with the <tt>connect()</tt> function in the Berkeley socket library.</p>
 
70
<p>This <a href="qobject.html#connect">connect()</a> call establishes a one-way connection between two Qt objects (objects that inherit <a href="qobject.html">QObject</a>, directly or indirectly). Every Qt object can have both <tt>signals</tt> (to send messages) and <tt>slots</tt> (to receive messages). All widgets are Qt objects, since they inherit <a href="qwidget.html">QWidget</a>, which in turn inherits <a href="qobject.html">QObject</a>.</p>
 
71
<p>Here, the <a href="qabstractbutton.html#clicked">clicked()</a> signal of <tt>quit</tt> is connected to the <a href="qcoreapplication.html#quit">quit()</a> slot of <tt>app</tt>, so that when the button is clicked, the application quits.</p>
 
72
<p>The <a href="signalsandslots.html">Signals and Slots</a> documentation describes this topic in detail.</p>
 
73
<a name="running-the-application"></a>
 
74
<h2>Running the Application</h2>
 
75
<p>When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.</p>
 
76
<p>See <a href="tutorial-t1.html">Chapter 1</a> for how to create a makefile and build the application.</p>
 
77
<a name="exercises"></a>
 
78
<h2>Exercises</h2>
 
79
<p>Try to resize the window. Press the button. Oops! That <a href="qobject.html#connect">connect()</a> would seem to make some difference.</p>
 
80
<p>Are there any other signals in <a href="qpushbutton.html">QPushButton</a> you can connect to quit? [Hint: The <a href="qpushbutton.html">QPushButton</a> inherits most of its functionality from <a href="qabstractbutton.html">QAbstractButton</a>.]</p>
 
81
<p>
 
82
[Previous: <a href="tutorial-t1.html">Chapter 1</a>]
 
83
[<a href="tutorial.html">Qt Tutorial</a>]
 
84
[Next: <a href="tutorial-t3.html">Chapter 3</a>]
 
85
</p>
 
86
<p /><address><hr /><div align="center">
 
87
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
88
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
89
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
90
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
91
</tr></table></div></address></body>
 
92
</html>