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

« back to all changes in this revision

Viewing changes to doc/html/widgets-wiggly.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/wiggly.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Wiggly Example</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
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<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>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Wiggly Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="widgets-wiggly-dialog-cpp.html">widgets/wiggly/dialog.cpp</a></li>
 
24
<li><a href="widgets-wiggly-dialog-h.html">widgets/wiggly/dialog.h</a></li>
 
25
<li><a href="widgets-wiggly-wigglywidget-cpp.html">widgets/wiggly/wigglywidget.cpp</a></li>
 
26
<li><a href="widgets-wiggly-wigglywidget-h.html">widgets/wiggly/wigglywidget.h</a></li>
 
27
<li><a href="widgets-wiggly-main-cpp.html">widgets/wiggly/main.cpp</a></li>
 
28
</ul>
 
29
<p>The Wiggly example shows how to animate a widget using <a href="qbasictimer.html">QBasicTimer</a> and <a href="qobject.html#timerEvent">timerEvent()</a>. In addition, the example demonstrates how to use <a href="qfontmetrics.html">QFontMetrics</a> to determine the size of text on screen.</p>
 
30
<center><img src="images/wiggly-example.png" alt="Screenshot of the Wiggly example" /></center><p><a href="qbasictimer.html">QBasicTimer</a> is a low-level class for timers. Unlike <a href="qtimer.html">QTimer</a>, <a href="qbasictimer.html">QBasicTimer</a> doesn't inherit from <a href="qobject.html">QObject</a>; instead of emitting a <a href="qtimer.html#timeout">timeout()</a> signal when a certain amount of time has passed, it sends a <a href="qtimerevent.html">QTimerEvent</a> to a <a href="qobject.html">QObject</a> of our choice. This makes <a href="qbasictimer.html">QBasicTimer</a> a more lightweight alternative to <a href="qtimer.html">QTimer</a>. Qt's built-in widgets use it internally, and it is provided in Qt's API for highly-optimized applications (e.g., Qt/Embedded applications).</p>
 
31
<p>The example consists of two classes:</p>
 
32
<ul>
 
33
<li><tt>WigglyWidget</tt> is the custom widget displaying the text in a wiggly line.</li>
 
34
<li><tt>Dialog</tt> is the dialog widget allowing the user to enter a text. It combines a <tt>WigglyWidget</tt> and a <tt>QLineEdit</tt>.</li>
 
35
</ul>
 
36
<p>We will first take a quick look at the <tt>Dialog</tt> class, then we will review the <tt>WigglyWidget</tt> class.</p>
 
37
<a name="dialog-class-definition"></a>
 
38
<h2>Dialog Class Definition</h2>
 
39
<pre>&nbsp;   class Dialog : public QDialog
 
40
    {
 
41
        Q_OBJECT
 
42
 
 
43
    public:
 
44
        Dialog(QWidget *parent = 0);
 
45
    };</pre>
 
46
<p>The <tt>Dialog</tt> class provides a dialog widget that allows the user to enter a text. The text is then rendeded by <tt>WigglyWidget</tt>.</p>
 
47
<a name="dialog-class-implementation"></a>
 
48
<h2>Dialog Class Implementation</h2>
 
49
<pre>&nbsp;   Dialog::Dialog(QWidget *parent)
 
50
        : QDialog(parent)
 
51
    {
 
52
        WigglyWidget *wigglyWidget = new WigglyWidget;
 
53
        QLineEdit *lineEdit = new QLineEdit;
 
54
 
 
55
        QVBoxLayout *layout = new QVBoxLayout;
 
56
        layout-&gt;addWidget(wigglyWidget);
 
57
        layout-&gt;addWidget(lineEdit);
 
58
        setLayout(layout);
 
59
 
 
60
        connect(lineEdit, SIGNAL(textChanged(QString)),
 
61
                wigglyWidget, SLOT(setText(QString)));
 
62
 
 
63
        lineEdit-&gt;setText(tr(&quot;Hello world!&quot;));
 
64
 
 
65
        setWindowTitle(tr(&quot;Wiggly&quot;));
 
66
        resize(360, 145);
 
67
    }</pre>
 
68
<p>In the constructor we create a wiggly widget along with a <a href="qlineedit.html">line edit</a>, and we put the two widgets in a vertical layout. We connect the line edit's <a href="qlineedit.html#textChanged">textChanged()</a> signal to the wiggly widget's <tt>setText()</tt> slot to obtain the real time interaction with the wiggly widget. The widget's default text is &quot;Hello world!&quot;.</p>
 
69
<a name="wigglywidget-class-definition"></a>
 
70
<h2>WigglyWidget Class Definition</h2>
 
71
<pre>&nbsp;   class WigglyWidget : public QWidget
 
72
    {
 
73
        Q_OBJECT
 
74
 
 
75
    public:
 
76
        WigglyWidget(QWidget *parent = 0);
 
77
 
 
78
    public slots:
 
79
        void setText(const QString &amp;newText) { text = newText; }
 
80
 
 
81
    protected:
 
82
        void paintEvent(QPaintEvent *event);
 
83
        void timerEvent(QTimerEvent *event);
 
84
 
 
85
    private:
 
86
        QBasicTimer timer;
 
87
        QString text;
 
88
        int step;
 
89
    };</pre>
 
90
<p>The <tt>WigglyWidget</tt> class provides the wiggly line displaying the text. We subclass <a href="qwidget.html">QWidget</a> and reimplement the standard <a href="qwidget.html#paintEvent">paintEvent()</a> and <a href="qobject.html#timerEvent">timerEvent()</a> functions to draw and update the widget. In addition we implement a public <tt>setText()</tt> slot that sets the widget's text.</p>
 
91
<p>The <tt>timer</tt> variable, of type <a href="qbasictimer.html">QBasicTimer</a>, is used to update the widget at regular intervals, making the widget move. The <tt>text</tt> variable is used to store the currently displayed text, and <tt>step</tt> to calculate position and color for each character on the wiggly line.</p>
 
92
<a name="wigglywidget-class-implementation"></a>
 
93
<h2>WigglyWidget Class Implementation</h2>
 
94
<pre>&nbsp;   WigglyWidget::WigglyWidget(QWidget *parent)
 
95
        : QWidget(parent)
 
96
    {
 
97
        setBackgroundRole(QPalette::Midlight);
 
98
 
 
99
        QFont newFont = font();
 
100
        newFont.setPointSize(newFont.pointSize() + 20);
 
101
        setFont(newFont);
 
102
 
 
103
        step = 0;
 
104
        timer.start(60, this);
 
105
    }</pre>
 
106
<p>In the constructor, we make the widget's background slightly lighter than the usual background using the <a href="qpalette.html#ColorRole-enum">QPalette::Midlight</a> color role. The background role defines the brush from the widget's palette that Qt uses to paint the background. Then we enlarge the widget's font with 20 points.</p>
 
107
<p>Finally we start the timer; the call to <a href="qbasictimer.html#start">QBasicTimer::start</a>() makes sure that <i>this</i> particular wiggly widget will receive the timer events generated when the timer times out (every 60 milliseconds).</p>
 
108
<pre>&nbsp;   void WigglyWidget::paintEvent(QPaintEvent * /* event */)
 
109
    {
 
110
        static const int sineTable[16] = {
 
111
            0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38
 
112
        };
 
113
 
 
114
        QFontMetrics metrics(font());
 
115
        int x = (width() - metrics.width(text)) / 2;
 
116
        int y = (height() + metrics.ascent() - metrics.descent()) / 2;
 
117
        QColor color;</pre>
 
118
<p>The <tt>paintEvent()</tt> function is called whenever a <a href="qpaintevent.html">QPaintEvent</a> is sent to the widget. Paint events are sent to widgets that need to update themselves, for instance when part of a widget is exposed because a covering widget was moved. For the wiggly widget, a paint event will also be generated every 60 milliseconds from the <tt>timerEvent()</tt> slot.</p>
 
119
<p>The <tt>sineTable</tt> represents y-values of the sine curve, multiplied by 100. It is used to make the wiggly widget move along the sine curve.</p>
 
120
<p>The <a href="qfontmetrics.html">QFontMetrics</a> object provides information about the widget's font. The <tt>x</tt> variable is the horizontal position where we start drawing the text. The <tt>y</tt> variable is the vertical position of the text's base line. Both variables are computed so that the text is horizontally and vertically centered. To compute the base line, we take into account the font's ascent (the height of the font above the base line) and font's descent (the height of the font below the base line). If the descent equals the ascent, they cancel out each other and the base line is at <tt>height()</tt> / 2.</p>
 
121
<pre>&nbsp;       QPainter painter(this);
 
122
        for (int i = 0; i &lt; text.size(); ++i) {
 
123
            int index = (step + i) % 16;
 
124
            color.setHsv((15 - index) * 16, 255, 191);
 
125
            painter.setPen(color);
 
126
            painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400),
 
127
                             QString(text[i]));
 
128
            x += metrics.width(text[i]);
 
129
        }
 
130
    }</pre>
 
131
<p>Each time the <tt>paintEvent()</tt> function is called, we create a <a href="qpainter.html">QPainter</a> object <tt>painter</tt> to draw the contents of the widget. For each character in <tt>text</tt>, we determine the color and the position on the wiggly line based on <tt>step</tt>. In addition, <tt>x</tt> is incremented by the character's width.</p>
 
132
<p>For simplicity, we assume that QFontMetrics::width(<tt>text</tt>) returns the sum of the individual character widths (QFontMetrics::width(<tt>text[i]</tt>)). In practice, this is not always the case because QFontMetrics::width(<tt>text</tt>) also takes into account the kerning between certain letters (e.g., 'A' and 'V'). The result is that the text isn't perfectly centered. You can verify this by typing &quot;AVAVAVAVAVAV&quot; in the line edit.</p>
 
133
<pre>&nbsp;   void WigglyWidget::timerEvent(QTimerEvent *event)
 
134
    {
 
135
        if (event-&gt;timerId() == timer.timerId()) {
 
136
            ++step;
 
137
            update();
 
138
        } else {
 
139
            QWidget::timerEvent(event);
 
140
        }</pre>
 
141
<p>The <tt>timerEvent()</tt> function receive all the timer events that are generated for this widget. If a timer event is sent from the widget's <a href="qbasictimer.html">QBasicTimer</a>, we increment <tt>step</tt> to make the text move, and call <a href="qwidget.html#update">QWidget::update</a>() to refresh the display. Any other timer event is passed on to the base class's implementation of the <a href="qobject.html#timerEvent">timerEvent()</a> function.</p>
 
142
<p>The <a href="qwidget.html#update">QWidget::update</a>() slot does not cause an immediate repaint; instead the slot schedules a paint event for processing when Qt returns to the main event loop. The paint events are then handled by <tt>WigglyWidget</tt>'s <tt>paintEvent()</tt> function.</p>
 
143
<p /><address><hr /><div align="center">
 
144
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
145
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
146
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
147
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
148
</tr></table></div></address></body>
 
149
</html>