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

« back to all changes in this revision

Viewing changes to doc/html/tutorial-t4.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 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" />
 
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-t3.html">Chapter 3</a>]
 
25
[<a href="tutorial.html">Qt Tutorial</a>]
 
26
[Next: <a href="tutorial-t5.html">Chapter 5</a>]
 
27
</p>
 
28
<h1 align="center">Qt Tutorial 4 - Let There Be Widgets</h1>
 
29
<p>Files:</p>
 
30
<ul>
 
31
<li><a href="tutorial-t4-main-cpp.html">tutorial/t4/main.cpp</a></li>
 
32
</ul>
 
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>&nbsp;   /****************************************************************
 
35
    **
 
36
    ** Qt tutorial 4
 
37
    **
 
38
    ****************************************************************/
 
39
 
 
40
    #include &lt;QApplication&gt;
 
41
    #include &lt;QFont&gt;
 
42
    #include &lt;QPushButton&gt;
 
43
 
 
44
    class MyWidget : public QWidget
 
45
    {
 
46
    public:
 
47
        MyWidget(QWidget *parent = 0);
 
48
    };
 
49
 
 
50
    MyWidget::MyWidget(QWidget *parent)
 
51
        : QWidget(parent)
 
52
    {
 
53
        setFixedSize(200, 120);
 
54
 
 
55
        QPushButton *quit = new QPushButton(&quot;Quit&quot;, this);
 
56
        quit-&gt;setGeometry(62, 40, 75, 30);
 
57
        quit-&gt;setFont(QFont(&quot;Times&quot;, 18, QFont::Bold));
 
58
 
 
59
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
 
60
    }
 
61
 
 
62
    int main(int argc, char *argv[])
 
63
    {
 
64
        QApplication app(argc, argv);
 
65
        MyWidget widget;
 
66
        widget.show();
 
67
        return app.exec();
 
68
    }</pre>
 
69
<a name="line-by-line-walkthrough"></a>
 
70
<h2>Line by Line Walkthrough</h2>
 
71
<pre>&nbsp;   class MyWidget : public QWidget
 
72
    {
 
73
    public:
 
74
        MyWidget(QWidget *parent = 0);
 
75
    };</pre>
 
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>&nbsp;   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>
 
82
<pre>&nbsp;   {
 
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>&nbsp;       QPushButton *quit = new QPushButton(&quot;Quit&quot;, this);
 
86
        quit-&gt;setGeometry(62, 40, 75, 30);
 
87
        quit-&gt;setFont(QFont(&quot;Times&quot;, 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>&nbsp;       connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
 
92
    }</pre>
 
93
<p>The <tt>qApp</tt> pointer is a global variable declared in the <tt>&lt;QApplication&gt;</tt> header file. It points to the application's unique <a href="qapplication.html">QApplication</a> instance.</p>
 
94
<pre>&nbsp;   int main(int argc, char *argv[])
 
95
    {
 
96
        QApplication app(argc, argv);
 
97
        MyWidget widget;
 
98
        widget.show();
 
99
        return app.exec();
 
100
    }</pre>
 
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>
 
106
<h2>Exercises</h2>
 
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>
 
109
<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>]
 
113
</p>
 
114
<p /><address><hr /><div align="center">
 
115
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
116
<td width="30%">Copyright &copy; 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>
 
120
</html>