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 4 - Let There Be Widgets</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-t3.html" />
15
<link rel="contents" href="tutorial.html" />
16
<link rel="next" href="tutorial-t5.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-t3.html">Chapter 3</a>]
25
[<a href="tutorial.html">Qt Tutorial</a>]
26
[Next: <a href="tutorial-t5.html">Chapter 5</a>]
28
<h1 align="center">Qt Tutorial 4 - Let There Be Widgets</h1>
31
<li><a href="tutorial-t4-main-cpp.html">tutorial/t4/main.cpp</a></li>
33
<center><img src="images/t4.png" alt="Screenshot of tutorial four" /></center><p>This example shows how to create your own widget, and describes how to control the minimum and maximum sizes of a widget.</p>
34
<pre> /****************************************************************
38
****************************************************************/
40
#include <QApplication>
41
#include <QFont>
42
#include <QPushButton>
44
class MyWidget : public QWidget
47
MyWidget(QWidget *parent = 0);
50
MyWidget::MyWidget(QWidget *parent)
53
setFixedSize(200, 120);
55
QPushButton *quit = new QPushButton("Quit", this);
56
quit->setGeometry(62, 40, 75, 30);
57
quit->setFont(QFont("Times", 18, QFont::Bold));
59
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
62
int main(int argc, char *argv[])
64
QApplication app(argc, argv);
69
<a name="line-by-line-walkthrough"></a>
70
<h2>Line by Line Walkthrough</h2>
71
<pre> class MyWidget : public QWidget
74
MyWidget(QWidget *parent = 0);
76
<p>Here we create a new class. Because this class inherits from <a href="qwidget.html">QWidget</a>, the new class is a widget and may be a top-level window or a child widget (like the <a href="qpushbutton.html">QPushButton</a> in the previous chapter).</p>
77
<p>This class has only one member, a constructor (in addition to the members it inherits from <a href="qwidget.html">QWidget</a>). The constructor is a standard Qt widget constructor; you should always include a similar constructor when you create widgets.</p>
78
<p>The argument is its parent widget. To create a top-level window you specify a null pointer as the parent. As you can see, this widget defaults to be a top-level window.</p>
79
<pre> MyWidget::MyWidget(QWidget *parent)
80
: QWidget(parent)</pre>
81
<p>The implementation of the constructor starts here. Like most widgets, it just passes on the <tt>parent</tt> to the <a href="qwidget.html">QWidget</a> constructor.</p>
83
setFixedSize(200, 120);</pre>
84
<p>Because this widget doesn't know how to handle resizing, we fix its size. In the next chapter, we will show how a widget can respond to resize event from the user.</p>
85
<pre> QPushButton *quit = new QPushButton("Quit", this);
86
quit->setGeometry(62, 40, 75, 30);
87
quit->setFont(QFont("Times", 18, QFont::Bold));</pre>
88
<p>Here we create and set up a child widget of this widget (the new widget's parent is <tt>this</tt>, i.e. the <tt>MyWidget</tt> instance).</p>
89
<p>Note that <tt>quit</tt> is a local variable in the constructor. <tt>MyWidget</tt> does not keep track of it; Qt does, and will automatically delete it when the <tt>MyWidget</tt> object is deleted. This is why <tt>MyWidget</tt> doesn't need a destructor. (On the other hand, there is no harm in deleting a child when you choose to. The child will automatically tell Qt about its imminent death.)</p>
90
<p>The QObject::setGeometry() call sets both the widget's screen position and the size. It is equivalent to calling <a href="qwidget.html#pos-prop">QWidget::move</a>() followed by <a href="qwidget.html#size-prop">QWidget::resize</a>().</p>
91
<pre> connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
93
<p>The <tt>qApp</tt> pointer is a global variable declared in the <tt><QApplication></tt> header file. It points to the application's unique <a href="qapplication.html">QApplication</a> instance.</p>
94
<pre> int main(int argc, char *argv[])
96
QApplication app(argc, argv);
101
<p>Here we instantiate our new child, set it to be the main widget, and execute the application.</p>
102
<a name="running-the-application"></a>
103
<h2>Running the Application</h2>
104
<p>This program is very similar in behavior to the previous one. The difference lies in the way we have implemented it. It does behave slightly differently, however. Just try to resize it to see.</p>
105
<a name="exercises"></a>
107
<p>Try to create another <tt>MyWidget</tt> object in <tt>main()</tt>. What happens?</p>
108
<p>Try to add more buttons or put in widgets other than <a href="qpushbutton.html">QPushButton</a>.</p>
110
[Previous: <a href="tutorial-t3.html">Chapter 3</a>]
111
[<a href="tutorial.html">Qt Tutorial</a>]
112
[Next: <a href="tutorial-t5.html">Chapter 5</a>]
114
<p /><address><hr /><div align="center">
115
<table width="100%" cellspacing="0" border="0"><tr class="address">
116
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
117
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
118
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
119
</tr></table></div></address></body>