1
<?xml version="1.0" encoding="iso-8859-1"?>
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 -->
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" />
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <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>]
28
<h1 align="center">Qt Tutorial 2 - Calling it Quits</h1>
31
<li><a href="tutorial-t2-main-cpp.html">tutorial/t2/main.cpp</a></li>
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> /****************************************************************
39
****************************************************************/
41
#include <QApplication>
42
#include <QFont>
43
#include <QPushButton>
45
int main(int argc, char *argv[])
47
QApplication app(argc, argv);
49
QPushButton quit("Quit");
51
quit.setFont(QFont("Times", 18, QFont::Bold));
53
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
58
<a name="line-by-line-walkthrough"></a>
59
<h2>Line by Line Walkthrough</h2>
60
<pre> #include <QFont></pre>
61
<p>Since this program uses <a href="qfont.html">QFont</a>, it needs to include <tt><QFont></tt>.</p>
62
<pre> QPushButton quit("Quit");</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> quit.resize(75, 30);</pre>
65
<p>We've chosen another size for the button since the text is a bit shorter than "Hello world!". 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> quit.setFont(QFont("Times", 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> QObject::connect(&quit, SIGNAL(clicked()), &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>
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>
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>]
86
<p /><address><hr /><div align="center">
87
<table width="100%" cellspacing="0" border="0"><tr class="address">
88
<td width="30%">Copyright © 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>