source: trunk/doc/html/richtext-example.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 10.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/richtext/richtext.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Richtext</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Richtext</h1>
33
34
35<p>
36In this example we demonstrate how to display rich text
37in a widget. To do this some sayings taken from the famous
38Unix "fortune" are displayed nicely formatted.
39<p> <hr>
40<p> Header file:
41<p> <pre>/****************************************************************************
42** $Id: richtext-example.html 2051 2007-02-21 10:04:20Z chehrlic $
43**
44** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
45**
46** This file is part of an example program for Qt. This example
47** program may be used, distributed and modified without limitation.
48**
49*****************************************************************************/
50
51#ifndef RICHTEXT_H
52#define RICHTEXT_H
53
54#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
55
56class QTextView;
57class QPushButton;
58
59class MyRichText : public <a href="qvbox.html">QVBox</a>
60{
61 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
62
63public:
64 MyRichText( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
65
66protected:
67 <a href="qtextview.html">QTextView</a> *view;
68 <a href="qpushbutton.html">QPushButton</a> *bClose, *bNext, *bPrev;
69 int num;
70
71protected slots:
72 void prev();
73 void next();
74
75};
76
77#endif
78</pre>
79
80<p> <hr>
81<p> Implementation:
82<p> <pre>/****************************************************************************
83** $Id: richtext-example.html 2051 2007-02-21 10:04:20Z chehrlic $
84**
85** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
86**
87** This file is part of an example program for Qt. This example
88** program may be used, distributed and modified without limitation.
89**
90*****************************************************************************/
91
92#include "richtext.h"
93
94#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
95#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
96#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
97#include &lt;<a href="qtextview-h.html">qtextview.h</a>&gt;
98#include &lt;<a href="qbrush-h.html">qbrush.h</a>&gt;
99#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
100
101static const char* sayings[] = {
102 "&lt;b&gt;Saying 1:&lt;/b&gt;&lt;br&gt;"
103 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
104 "&lt;big&gt;Evil is that which one believes of others. It is a sin to believe evil "
105 "of others, but it is seldom a mistake.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
106 "&lt;center&gt;&lt;i&gt;-- H.L. Mencken&lt;/i&gt;&lt;/center&gt;",
107
108 "&lt;b&gt;Saying 2:&lt;/b&gt;&lt;br&gt;"
109 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
110 "&lt;big&gt;A well-used door needs no oil on its hinges.&lt;br&gt;"
111 "A swift-flowing steam does not grow stagnant.&lt;br&gt;"
112 "Neither sound nor thoughts can travel through a vacuum.&lt;br&gt;"
113 "Software rots if not used.&lt;br&gt;&lt;br&gt;"
114 "These are great mysteries.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
115 "&lt;center&gt;&lt;i&gt;-- Geoffrey James, \"The Tao of Programming\"&lt;/i&gt;&lt;/center&gt;",
116
117 "&lt;b&gt;Saying 3:&lt;/b&gt;&lt;br&gt;"
118 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
119 "&lt;big&gt;Show business is just like high school, except you get paid.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
120 "&lt;center&gt;&lt;i&gt;-- Martin Mull&lt;/i&gt;&lt;/center&gt;",
121
122 "&lt;b&gt;Saying 4:&lt;/b&gt;&lt;br&gt;"
123 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
124 "&lt;big&gt;&lt;b&gt;The Least Successful Executions&lt;/b&gt;&lt;br&gt;"
125 "&lt;twocolumn&gt;&lt;p&gt; History has furnished us with two executioners worthy of attention. "
126 "The first performed in Sydney in Australia. In 1803 three attempts were "
127 "made to hang a Mr. Joseph Samuels. On the first two of these the rope "
128 "snapped, while on the third Mr. Samuels just hung there peacefully until he "
129 "and everyone else got bored. Since he had proved unsusceptible to capital "
130 "punishment, he was reprieved.&lt;/p&gt;"
131 "&lt;p&gt; The most important British executioner was Mr. James Berry who "
132 "tried three times in 1885 to hang Mr. John Lee at Exeter Jail, but on each "
133 "occasion failed to get the trap door open.&lt;!p&gt;"
134 "&lt;p&gt; In recognition of this achievement, the Home Secretary commuted "
135 "Lee's sentence to \"life\" imprisonment. He was released in 1917, emigrated "
136 "to America and lived until 1933.&lt;/p&gt;&lt;/twocolumn&gt;&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
137 "&lt;center&gt;&lt;i&gt;-- Stephen Pile, \"The Book of Heroic Failures\"&lt;/i&gt;&lt;/center&gt;",
138
139 "&lt;b&gt;Saying 5:&lt;/b&gt;&lt;br&gt;"
140 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
141 "&lt;big&gt;If you can, help others. If you can't, at least don't hurt others.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
142 "&lt;center&gt;&lt;i&gt;-- the Dalai Lama&lt;/i&gt;&lt;/center&gt;",
143
144 "&lt;b&gt;Saying 6:&lt;/b&gt;&lt;br&gt;"
145 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
146 "&lt;big&gt;Television has brought back murder into the home -- where it belongs.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
147 "&lt;center&gt;&lt;i&gt;-- Alfred Hitchcock&lt;/i&gt;&lt;/center&gt;",
148
149 "&lt;b&gt;Saying 7:&lt;/b&gt;&lt;br&gt;"
150 "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
151 "&lt;big&gt;I don't know who my grandfather was; I am much more concerned to know "
152 "what his grandson will be.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
153 "&lt;center&gt;&lt;i&gt;-- Abraham Lincoln&lt;/i&gt;&lt;/center&gt;",
154
155 0
156};
157
158
159<a name="f255"></a>MyRichText::MyRichText( <a href="qwidget.html">QWidget</a> *parent, const char *name )
160 : <a href="qvbox.html">QVBox</a>( parent, name )
161{
162 <a href="qframe.html#setMargin">setMargin</a>( 5 );
163
164 view = new <a href="qtextview.html">QTextView</a>( this );
165<a name="x465"></a> view-&gt;<a href="qtextedit.html#setText">setText</a>( "This is a &lt;b&gt;Test&lt;/b&gt; with &lt;i&gt;italic&lt;/i&gt; &lt;u&gt;stuff&lt;/u&gt;" );
166 <a href="qbrush.html">QBrush</a> paper;
167<a name="x461"></a> paper.<a href="qbrush.html#setPixmap">setPixmap</a>( QPixmap( "../richtext/marble.png" ) );
168<a name="x460"></a> if ( paper.<a href="qbrush.html#pixmap">pixmap</a>() != 0 )
169<a name="x464"></a> view-&gt;<a href="qtextedit.html#setPaper">setPaper</a>( paper );
170 else
171 view-&gt;<a href="qtextedit.html#setPaper">setPaper</a>( white );
172
173 view-&gt;<a href="qtextedit.html#setText">setText</a>( sayings[0] );
174<a name="x467"></a> view-&gt;<a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 450, 250 );
175
176 <a href="qhbox.html">QHBox</a> *buttons = new <a href="qhbox.html">QHBox</a>( this );
177 buttons-&gt;<a href="qframe.html#setMargin">setMargin</a>( 5 );
178
179 bClose = new <a href="qpushbutton.html">QPushButton</a>( "&amp;Close", buttons );
180 bPrev = new <a href="qpushbutton.html">QPushButton</a>( "&lt;&lt; &amp;Prev", buttons );
181 bNext = new <a href="qpushbutton.html">QPushButton</a>( "&amp;Next &gt;&gt;", buttons );
182
183<a name="x466"></a> bPrev-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
184
185<a name="x462"></a> <a href="qobject.html#connect">connect</a>( bClose, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), qApp, SLOT( <a href="qapplication.html#quit">quit</a>() ) );
186 <a href="qobject.html#connect">connect</a>( bPrev, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( prev() ) );
187 <a href="qobject.html#connect">connect</a>( bNext, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( next() ) );
188
189 num = 0;
190}
191
192void <a name="f256"></a>MyRichText::prev()
193{
194 if ( num &lt;= 0 )
195 return;
196
197 num--;
198
199 view-&gt;<a href="qtextedit.html#setText">setText</a>( sayings[num] );
200
201 if ( num == 0 )
202 bPrev-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
203
204 bNext-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
205}
206
207void <a name="f257"></a>MyRichText::next()
208{
209 if ( !sayings[++num] )
210 return;
211
212 view-&gt;<a href="qtextedit.html#setText">setText</a>( sayings[num] );
213
214 if ( !sayings[num + 1] )
215 bNext-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );
216
217 bPrev-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
218}
219
220
221
222
223
224</pre>
225
226<p> <hr>
227<p> Main:
228<p> <pre>/****************************************************************************
229** $Id: richtext-example.html 2051 2007-02-21 10:04:20Z chehrlic $
230**
231** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
232**
233** This file is part of an example program for Qt. This example
234** program may be used, distributed and modified without limitation.
235**
236*****************************************************************************/
237
238#include "richtext.h"
239#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
240
241int main( int argc, char **argv )
242{
243 <a href="qapplication.html">QApplication</a> a( argc, argv );
244
245 MyRichText richtext;
246 richtext.<a href="qwidget.html#resize">resize</a>( 450, 350 );
247 richtext.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Richtext" );
248 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;richtext );
249 richtext.<a href="qwidget.html#show">show</a>();
250
251 return a.<a href="qapplication.html#exec">exec</a>();
252}
253</pre>
254
255<p>See also <a href="examples.html">Examples</a>.
256
257<!-- eof -->
258<p><address><hr><div align=center>
259<table width=100% cellspacing=0 border=0><tr>
260<td>Copyright &copy; 2007
261<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
262<td align=right><div align=right>Qt 3.3.8</div>
263</table></div></address></body>
264</html>
Note: See TracBrowser for help on using the repository browser.