summaryrefslogtreecommitdiffstats
path: root/doc/html/richtext-example.html
blob: 692e7a4836d2df333a9b0ec585573c0ed4ca3ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/richtext/richtext.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Richtext</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

   
<p> 
In this example we demonstrate how to display rich text
in a widget. To do this some sayings taken from the famous
Unix "fortune" are displayed nicely formatted.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/richtext.h   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef RICHTEXT_H
#define RICHTEXT_H

#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;

class TQTextView;
class TQPushButton;

class MyRichText : public <a href="qvbox.html">TQVBox</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

public:
    MyRichText( <a href="qwidget.html">TQWidget</a> *parent = 0, const char *name = 0 );

protected:
    <a href="qtextview.html">TQTextView</a> *view;
    <a href="qpushbutton.html">TQPushButton</a> *bClose, *bNext, *bPrev;
    int num;

protected slots:
    void prev();
    void next();

};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/richtext.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "richtext.h"

#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qtextview-h.html">qtextview.h</a>&gt;
#include &lt;<a href="qbrush-h.html">qbrush.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;

static const char* sayings[] = {
    "&lt;b&gt;Saying 1:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;big&gt;Evil is that which one believes of others.  It is a sin to believe evil "
    "of others, but it is seldom a mistake.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;center&gt;&lt;i&gt;-- H.L. Mencken&lt;/i&gt;&lt;/center&gt;",

    "&lt;b&gt;Saying 2:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;big&gt;A well-used door needs no oil on its hinges.&lt;br&gt;"
    "A swift-flowing steam does not grow stagnant.&lt;br&gt;"
    "Neither sound nor thoughts can travel through a vacuum.&lt;br&gt;"
    "Software rots if not used.&lt;br&gt;&lt;br&gt;"
    "These are great mysteries.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;center&gt;&lt;i&gt;-- Geoffrey James, \"The Tao of Programming\"&lt;/i&gt;&lt;/center&gt;",

    "&lt;b&gt;Saying 3:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;big&gt;Show business is just like high school, except you get paid.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;center&gt;&lt;i&gt;-- Martin Mull&lt;/i&gt;&lt;/center&gt;",

    "&lt;b&gt;Saying 4:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;big&gt;&lt;b&gt;The Least Successful Executions&lt;/b&gt;&lt;br&gt;"
    "&lt;twocolumn&gt;&lt;p&gt;      History has furnished us with two executioners worthy of attention. "
    "The first performed in Sydney in Australia.  In 1803 three attempts were "
    "made to hang a Mr. Joseph Samuels.  On the first two of these the rope "
    "snapped, while on the third Mr. Samuels just hung there peacefully until he "
    "and everyone else got bored.  Since he had proved unsusceptible to capital "
    "punishment, he was reprieved.&lt;/p&gt;"
    "&lt;p&gt;        The most important British executioner was Mr. James Berry who "
    "tried three times in 1885 to hang Mr. John Lee at Exeter Jail, but on each "
    "occasion failed to get the trap door open.&lt;!p&gt;"
    "&lt;p&gt;        In recognition of this achievement, the Home Secretary commuted "
    "Lee's sentence to \"life\" imprisonment.  He was released in 1917, emigrated "
    "to America and lived until 1933.&lt;/p&gt;&lt;/twocolumn&gt;&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;center&gt;&lt;i&gt;-- Stephen Pile, \"The Book of Heroic Failures\"&lt;/i&gt;&lt;/center&gt;",

    "&lt;b&gt;Saying 5:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&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;"
    "&lt;center&gt;&lt;i&gt;-- the Dalai Lama&lt;/i&gt;&lt;/center&gt;",

    "&lt;b&gt;Saying 6:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;big&gt;Television has brought back murder into the home -- where it belongs.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;center&gt;&lt;i&gt;-- Alfred Hitchcock&lt;/i&gt;&lt;/center&gt;",

    "&lt;b&gt;Saying 7:&lt;/b&gt;&lt;br&gt;"
    "&lt;hr&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;big&gt;I don't know who my grandfather was; I am much more concerned to know "
    "what his grandson will be.&lt;/big&gt;&lt;br&gt;&lt;br&gt;"
    "&lt;center&gt;&lt;i&gt;-- Abraham Lincoln&lt;/i&gt;&lt;/center&gt;",

    0
};


<a name="f255"></a>MyRichText::MyRichText( <a href="qwidget.html">TQWidget</a> *parent, const char *name )
    : <a href="qvbox.html">TQVBox</a>( parent, name )
{
    <a href="qframe.html#setMargin">setMargin</a>( 5 );

    view = new <a href="qtextview.html">TQTextView</a>( this );
<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;" );
    <a href="qbrush.html">TQBrush</a> paper;
<a name="x461"></a>    paper.<a href="qbrush.html#setPixmap">setPixmap</a>( TQPixmap( "../richtext/marble.png" ) );
<a name="x460"></a>    if ( paper.<a href="qbrush.html#pixmap">pixmap</a>() != 0 )
<a name="x464"></a>        view-&gt;<a href="qtextedit.html#setPaper">setPaper</a>( paper );
    else
        view-&gt;<a href="qtextedit.html#setPaper">setPaper</a>( white );

    view-&gt;<a href="qtextedit.html#setText">setText</a>( sayings[0] );
<a name="x467"></a>    view-&gt;<a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 450, 250 );

    <a href="qhbox.html">TQHBox</a> *buttons = new <a href="qhbox.html">TQHBox</a>( this );
    buttons-&gt;<a href="qframe.html#setMargin">setMargin</a>( 5 );

    bClose = new <a href="qpushbutton.html">TQPushButton</a>( "&amp;Close", buttons );
    bPrev = new <a href="qpushbutton.html">TQPushButton</a>( "&lt;&lt; &amp;Prev", buttons );
    bNext = new <a href="qpushbutton.html">TQPushButton</a>( "&amp;Next &gt;&gt;", buttons );

<a name="x466"></a>    bPrev-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );

<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#tquit">tquit</a>() ) );
    <a href="qobject.html#connect">connect</a>( bPrev, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( prev() ) );
    <a href="qobject.html#connect">connect</a>( bNext, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( next() ) );

    num = 0;
}

void <a name="f256"></a>MyRichText::prev()
{
    if ( num &lt;= 0 )
        return;

    num--;

    view-&gt;<a href="qtextedit.html#setText">setText</a>( sayings[num] );

    if ( num == 0 )
        bPrev-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );

    bNext-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
}

void <a name="f257"></a>MyRichText::next()
{
    if ( !sayings[++num] )
        return;

    view-&gt;<a href="qtextedit.html#setText">setText</a>( sayings[num] );

    if ( !sayings[num + 1] )
        bNext-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( FALSE );

    bPrev-&gt;<a href="qwidget.html#setEnabled">setEnabled</a>( TRUE );
}





</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "richtext.h"
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;

int main( int argc, char **argv )
{
    <a href="qapplication.html">TQApplication</a> a( argc, argv );

    MyRichText richtext;
    richtext.<a href="qwidget.html#resize">resize</a>( 450, 350 );
    richtext.<a href="qwidget.html#setCaption">setCaption</a>( "TQt Example - Richtext" );
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;richtext );
    richtext.<a href="qwidget.html#show">show</a>();

    return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>