source: trunk/doc/html/simple-font-demo-example.html

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

reference documentation added

File size: 11.8 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/fonts/simple-qfont-demo/simple-qfont-demo.doc:1 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>A simple demonstration of QFont member functions</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>A simple demonstration of QFont member functions</h1>
33
34
35<p>
36<p> This example demonstrates the use of various
37<a href="qfont.html">QFont</a> member functions.
38<p> <hr>
39<p> The main window API (viewer.h):
40<p> <pre>/* $Id: simple-font-demo-example.html 2051 2007-02-21 10:04:20Z chehrlic $ */
41
42#ifndef VIEWER_H
43#define VIEWER_H
44
45
46#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
47#include &lt;<a href="qfont-h.html">qfont.h</a>&gt;
48
49class QTextView;
50class QPushButton;
51
52class Viewer : public <a href="qwidget.html">QWidget</a>
53{
54<a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
55
56public:
57 Viewer();
58
59private slots:
60 void setDefault();
61 void setSansSerif();
62 void setItalics();
63
64private:
65 void setFontSubstitutions();
66 void layout();
67 void showFontInfo( <a href="qfont.html">QFont</a> &amp; );
68
69 <a href="qtextview.html">QTextView</a> * greetings;
70 <a href="qtextview.html">QTextView</a> * fontInfo;
71
72 <a href="qpushbutton.html">QPushButton</a> * defaultButton;
73 <a href="qpushbutton.html">QPushButton</a> * sansSerifButton;
74 <a href="qpushbutton.html">QPushButton</a> * italicsButton;
75};
76
77#endif
78</pre>
79
80<p> <hr>
81<p> The main window implementation (viewer.cpp):
82<p> <pre>/* $Id: simple-font-demo-example.html 2051 2007-02-21 10:04:20Z chehrlic $ */
83
84#include "viewer.h"
85#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
86#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;
87#include &lt;<a href="qtextview-h.html">qtextview.h</a>&gt;
88#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
89#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
90
91<a name="f590"></a>Viewer::Viewer()
92 :<a href="qwidget.html">QWidget</a>()
93{
94 setFontSubstitutions();
95
96<a name="x2850"></a> <a href="qstring.html">QString</a> greeting_heb = QString::<a href="qstring.html#fromUtf8">fromUtf8</a>( "\327\251\327\234\327\225\327\235" );
97 <a href="qstring.html">QString</a> greeting_ru = QString::<a href="qstring.html#fromUtf8">fromUtf8</a>( "\320\227\320\264\321\200\320\260\320\262\321\201\321\202\320\262\321\203\320\271\321\202\320\265" );
98 <a href="qstring.html">QString</a> greeting_en( "Hello" );
99
100 greetings = new <a href="qtextview.html">QTextView</a>( this, "textview" );
101
102 greetings-&gt;<a href="qtextedit.html#setText">setText</a>( greeting_en + "\n" +
103 greeting_ru + "\n" +
104 greeting_heb );
105
106 fontInfo = new <a href="qtextview.html">QTextView</a>( this, "fontinfo" );
107
108 setDefault();
109
110 defaultButton = new <a href="qpushbutton.html">QPushButton</a>( "Default", this,
111 "pushbutton1" );
112<a name="x2860"></a> defaultButton-&gt;<a href="qwidget.html#setFont">setFont</a>( QFont( "times" ) );
113 <a href="qobject.html#connect">connect</a>( defaultButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ),
114 this, SLOT( setDefault() ) );
115
116 sansSerifButton = new <a href="qpushbutton.html">QPushButton</a>( "Sans Serif", this,
117 "pushbutton2" );
118 sansSerifButton-&gt;<a href="qwidget.html#setFont">setFont</a>( QFont( "Helvetica", 12 ) );
119 <a href="qobject.html#connect">connect</a>( sansSerifButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ),
120 this, SLOT( setSansSerif() ) );
121
122 italicsButton = new <a href="qpushbutton.html">QPushButton</a>( "Italics", this,
123 "pushbutton3" );
124 italicsButton-&gt;<a href="qwidget.html#setFont">setFont</a>( QFont( "lucida", 12, QFont::Bold, TRUE ) );
125 <a href="qobject.html#connect">connect</a>( italicsButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ),
126 this, SLOT( setItalics() ) );
127
128 <a href="qwidget.html#layout">layout</a>();
129}
130
131void <a name="f591"></a>Viewer::setDefault()
132{
133 <a href="qfont.html">QFont</a> font( "Bavaria" );
134<a name="x2843"></a> font.<a href="qfont.html#setPointSize">setPointSize</a>( 24 );
135
136<a name="x2846"></a> font.<a href="qfont.html#setWeight">setWeight</a>( QFont::Bold );
137<a name="x2845"></a> font.<a href="qfont.html#setUnderline">setUnderline</a>( TRUE );
138
139 greetings-&gt;<a href="qwidget.html#setFont">setFont</a>( font );
140
141 showFontInfo( font );
142}
143
144void <a name="f592"></a>Viewer::setSansSerif()
145{
146 <a href="qfont.html">QFont</a> font( "Newyork", 18 );
147<a name="x2844"></a> font.<a href="qfont.html#setStyleHint">setStyleHint</a>( QFont::SansSerif );
148 greetings-&gt;<a href="qwidget.html#setFont">setFont</a>( font );
149
150 showFontInfo( font );
151}
152
153void <a name="f593"></a>Viewer::setItalics()
154{
155 <a href="qfont.html">QFont</a> font( "Tokyo" );
156 font.<a href="qfont.html#setPointSize">setPointSize</a>( 32 );
157 font.<a href="qfont.html#setWeight">setWeight</a>( QFont::Bold );
158<a name="x2842"></a> font.<a href="qfont.html#setItalic">setItalic</a>( TRUE );
159
160 greetings-&gt;<a href="qwidget.html#setFont">setFont</a>( font );
161
162 showFontInfo( font );
163}
164
165void <a name="f594"></a>Viewer::showFontInfo( <a href="qfont.html">QFont</a> &amp; font )
166{
167 <a href="qfontinfo.html">QFontInfo</a> info( font );
168
169 <a href="qstring.html">QString</a> messageText;
170 messageText = "Font requested: \"" +
171<a name="x2838"></a> font.<a href="qfont.html#family">family</a>() + "\" " +
172<a name="x2851"></a><a name="x2841"></a> QString::<a href="qstring.html#number">number</a>( font.<a href="qfont.html#pointSize">pointSize</a>() ) + "pt&lt;BR&gt;" +
173 "Font used: \"" +
174<a name="x2848"></a> info.<a href="qfontinfo.html#family">family</a>() + "\" " +
175<a name="x2849"></a> QString::<a href="qstring.html#number">number</a>( info.<a href="qfontinfo.html#pointSize">pointSize</a>() ) + "pt&lt;P&gt;";
176
177<a name="x2847"></a> <a href="qstringlist.html">QStringList</a> substitutions = QFont::<a href="qfont.html#substitutes">substitutes</a>( font.<a href="qfont.html#family">family</a>() );
178
179<a name="x2856"></a> if ( ! substitutions.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ){
180 messageText += "The following substitutions exist for " + \
181 font.<a href="qfont.html#family">family</a>() + ":&lt;UL&gt;";
182
183 QStringList::Iterator i = substitutions.<a href="qvaluelist.html#begin">begin</a>();
184 while ( i != substitutions.<a href="qvaluelist.html#end">end</a>() ){
185 messageText += "&lt;LI&gt;\"" + (* i) + "\"";
186 i++;
187 }
188 messageText += "&lt;/UL&gt;";
189 } else {
190 messageText += "No substitutions exist for " + \
191 font.<a href="qfont.html#family">family</a>() + ".";
192 }
193
194 fontInfo-&gt;<a href="qtextedit.html#setText">setText</a>( messageText );
195}
196
197void <a name="f595"></a>Viewer::setFontSubstitutions()
198{
199 <a href="qstringlist.html">QStringList</a> substitutes;
200<a name="x2853"></a> substitutes.<a href="qvaluelist.html#append">append</a>( "Times" );
201 substitutes += "Mincho",
202 substitutes &lt;&lt; "Arabic Newspaper" &lt;&lt; "crox";
203
204<a name="x2840"></a> QFont::<a href="qfont.html#insertSubstitutions">insertSubstitutions</a>( "Bavaria", substitutes );
205
206<a name="x2839"></a> QFont::<a href="qfont.html#insertSubstitution">insertSubstitution</a>( "Tokyo", "Lucida" );
207}
208
209
210// For those who prefer to use Qt Designer for creating GUIs
211// the following function might not be of particular interest:
212// all it does is creating the widget layout.
213
214<a name="x2858"></a>void Viewer::<a href="qwidget.html#layout">layout</a>()
215{
216 <a href="qhboxlayout.html">QHBoxLayout</a> * textViewContainer = new <a href="qhboxlayout.html">QHBoxLayout</a>();
217 textViewContainer-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( greetings );
218 textViewContainer-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( fontInfo );
219
220 <a href="qhboxlayout.html">QHBoxLayout</a> * buttonContainer = new <a href="qhboxlayout.html">QHBoxLayout</a>();
221
222 buttonContainer-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( defaultButton );
223 buttonContainer-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( sansSerifButton );
224 buttonContainer-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( italicsButton );
225
226<a name="x2857"></a> int maxButtonHeight = defaultButton-&gt;<a href="qwidget.html#height">height</a>();
227
228 if ( sansSerifButton-&gt;<a href="qwidget.html#height">height</a>() &gt; maxButtonHeight )
229 maxButtonHeight = sansSerifButton-&gt;<a href="qwidget.html#height">height</a>();
230 if ( italicsButton-&gt;<a href="qwidget.html#height">height</a>() &gt; maxButtonHeight )
231 maxButtonHeight = italicsButton-&gt;<a href="qwidget.html#height">height</a>();
232
233<a name="x2859"></a> defaultButton-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( maxButtonHeight );
234 sansSerifButton-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( maxButtonHeight );
235 italicsButton-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( maxButtonHeight );
236
237 <a href="qvboxlayout.html">QVBoxLayout</a> * container = new <a href="qvboxlayout.html">QVBoxLayout</a>( this );
238<a name="x2835"></a> container-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( textViewContainer );
239 container-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( buttonContainer );
240
241 <a href="qwidget.html#resize">resize</a>( 700, 250 );
242}
243</pre>
244
245<p> <hr>
246<p> main() program (simple-qfont-demo.cpp):
247<p> <pre>/* $Id: simple-font-demo-example.html 2051 2007-02-21 10:04:20Z chehrlic $ */
248
249#include "viewer.h"
250
251#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
252
253int main( int argc, char **argv )
254{
255 <a href="qapplication.html">QApplication</a> app( argc, argv );
256 Viewer * textViewer = new Viewer();
257 textViewer-&gt;<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Simple QFont Demo" );
258 app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( textViewer );
259 textViewer-&gt;<a href="qwidget.html#show">show</a>();
260 return app.<a href="qapplication.html#exec">exec</a>();
261}
262</pre>
263
264<p> <p>See also <a href="qfont-examples.html">QFont Examples</a>.
265
266<!-- eof -->
267<p><address><hr><div align=center>
268<table width=100% cellspacing=0 border=0><tr>
269<td>Copyright &copy; 2007
270<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
271<td align=right><div align=right>Qt 3.3.8</div>
272</table></div></address></body>
273</html>
Note: See TracBrowser for help on using the repository browser.