<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/fonts/simple-qfont-demo/simple-qfont-demo.doc:5 --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>A simple demonstration of QFont member functions</title> <style type="text/css"><!-- h3.fn,span.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 Classes</font></a> | <a href="mainclasses.html"> <font color="#004faf">Main Classes</font></a> | <a href="annotated.html"> <font color="#004faf">Annotated</font></a> | <a href="groups.html"> <font color="#004faf">Grouped 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>A simple demonstration of QFont member functions</h1> <p> <p> This example demonstrates the use of various <a href="qfont.html">QFont</a> member functions. <p> It is covered in detail by a <a href="simple-qfont-demo-walkthrough.html">walkthrough.</a> <p> <hr> <p> The main window API (viewer.h): <p> <pre>/* $Id$ */ #ifndef VIEWER_H #define VIEWER_H #include <<a href="qwidget-h.html">qwidget.h</a>> #include <<a href="qfont-h.html">qfont.h</a>> class QTextView; class QPushButton; class Viewer : public <a href="qwidget.html">QWidget</a> { <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> public: Viewer(); private slots: void setDefault(); void setSansSerif(); void setItalics(); private: void setFontSubstitutions(); void layout(); void showFontInfo( <a href="qfont.html">QFont</a> & ); <a href="qtextview.html">QTextView</a> * greetings; <a href="qtextview.html">QTextView</a> * fontInfo; <a href="qpushbutton.html">QPushButton</a> * defaultButton; <a href="qpushbutton.html">QPushButton</a> * sansSerifButton; <a href="qpushbutton.html">QPushButton</a> * italicsButton; }; #endif </pre> <p> <hr> <p> The main window implementation (viewer.cpp): <p> <pre>/* $Id$ */ #include "viewer.h" #include <<a href="qstring-h.html">qstring.h</a>> #include <<a href="qstringlist-h.html">qstringlist.h</a>> #include <<a href="qtextview-h.html">qtextview.h</a>> #include <<a href="qpushbutton-h.html">qpushbutton.h</a>> #include <<a href="qlayout-h.html">qlayout.h</a>> <a name="f427"></a>Viewer::Viewer() :<a href="qwidget.html">QWidget</a>() { setFontSubstitutions(); <a name="x1506"></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" ); <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" ); <a href="qstring.html">QString</a> greeting_en( "Hello" ); greetings = new <a href="qtextview.html">QTextView</a>( this, "textview" ); greetings-><a href="qtextedit.html#setText">setText</a>( greeting_en + "\n" + greeting_ru + "\n" + greeting_heb ); fontInfo = new <a href="qtextview.html">QTextView</a>( this, "fontinfo" ); setDefault(); defaultButton = new <a href="qpushbutton.html">QPushButton</a>( "Default", this, "pushbutton1" ); <a name="x1516"></a> defaultButton-><a href="qwidget.html#setFont">setFont</a>( QFont( "times" ) ); <a href="qobject.html#connect">connect</a>( defaultButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( setDefault() ) ); sansSerifButton = new <a href="qpushbutton.html">QPushButton</a>( "Sans Serif", this, "pushbutton2" ); sansSerifButton-><a href="qwidget.html#setFont">setFont</a>( QFont( "Helvetica", 12 ) ); <a href="qobject.html#connect">connect</a>( sansSerifButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( setSansSerif() ) ); italicsButton = new <a href="qpushbutton.html">QPushButton</a>( "Italics", this, "pushbutton3" ); italicsButton-><a href="qwidget.html#setFont">setFont</a>( QFont( "lucida", 12, QFont::Bold, TRUE ) ); <a href="qobject.html#connect">connect</a>( italicsButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( setItalics() ) ); <a href="qwidget.html#layout">layout</a>(); } void <a name="f428"></a>Viewer::setDefault() { <a href="qfont.html">QFont</a> font( "Bavaria" ); <a name="x1499"></a> font.<a href="qfont.html#setPointSize">setPointSize</a>( 24 ); <a name="x1502"></a> font.<a href="qfont.html#setWeight">setWeight</a>( QFont::Bold ); <a name="x1501"></a> font.<a href="qfont.html#setUnderline">setUnderline</a>( TRUE ); greetings-><a href="qwidget.html#setFont">setFont</a>( font ); showFontInfo( font ); } void <a name="f429"></a>Viewer::setSansSerif() { <a href="qfont.html">QFont</a> font( "Newyork", 18 ); <a name="x1500"></a> font.<a href="qfont.html#setStyleHint">setStyleHint</a>( QFont::SansSerif ); greetings-><a href="qwidget.html#setFont">setFont</a>( font ); showFontInfo( font ); } void <a name="f430"></a>Viewer::setItalics() { <a href="qfont.html">QFont</a> font( "Tokyo" ); font.<a href="qfont.html#setPointSize">setPointSize</a>( 32 ); font.<a href="qfont.html#setWeight">setWeight</a>( QFont::Bold ); <a name="x1498"></a> font.<a href="qfont.html#setItalic">setItalic</a>( TRUE ); greetings-><a href="qwidget.html#setFont">setFont</a>( font ); showFontInfo( font ); } void <a name="f431"></a>Viewer::showFontInfo( <a href="qfont.html">QFont</a> & font ) { <a href="qfontinfo.html">QFontInfo</a> info( font ); <a href="qstring.html">QString</a> messageText; messageText = "Font requested: \"" + <a name="x1494"></a> font.<a href="qfont.html#family">family</a>() + "\" " + <a name="x1507"></a><a name="x1497"></a> QString::<a href="qstring.html#number">number</a>( font.<a href="qfont.html#pointSize">pointSize</a>() ) + "pt<BR>" + "Font used: \"" + <a name="x1504"></a> info.<a href="qfontinfo.html#family">family</a>() + "\" " + <a name="x1505"></a> QString::<a href="qstring.html#number">number</a>( info.<a href="qfontinfo.html#pointSize">pointSize</a>() ) + "pt<P>"; <a name="x1503"></a> <a href="qstringlist.html">QStringList</a> substitutions = QFont::<a href="qfont.html#substitutes">substitutes</a>( font.<a href="qfont.html#family">family</a>() ); <a name="x1512"></a> if ( ! substitutions.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ){ messageText += "The following substitutions exist for " + \ font.<a href="qfont.html#family">family</a>() + ":<UL>"; QStringList::Iterator i = substitutions.<a href="qvaluelist.html#begin">begin</a>(); while ( i != substitutions.<a href="qvaluelist.html#end">end</a>() ){ messageText += "<LI>\"" + (* i) + "\""; i++; } messageText += "</UL>"; } else { messageText += "No substitutions exist for " + \ font.<a href="qfont.html#family">family</a>() + "."; } fontInfo-><a href="qtextedit.html#setText">setText</a>( messageText ); } void <a name="f432"></a>Viewer::setFontSubstitutions() { <a href="qstringlist.html">QStringList</a> substitutes; <a name="x1509"></a> substitutes.<a href="qvaluelist.html#append">append</a>( "Times" ); substitutes += "Mincho", substitutes << "Arabic Newspaper" << "crox"; <a name="x1496"></a> QFont::<a href="qfont.html#insertSubstitutions">insertSubstitutions</a>( "Bavaria", substitutes ); <a name="x1495"></a> QFont::<a href="qfont.html#insertSubstitution">insertSubstitution</a>( "Tokyo", "Lucida" ); } // For those who prefer to use Qt Designer for creating GUIs // the following function might not be of particular interest: // all it does is creating the widget layout. <a name="x1514"></a>void Viewer::<a href="qwidget.html#layout">layout</a>() { <a href="qhboxlayout.html">QHBoxLayout</a> * textViewContainer = new <a href="qhboxlayout.html">QHBoxLayout</a>(); textViewContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( greetings ); textViewContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( fontInfo ); <a href="qhboxlayout.html">QHBoxLayout</a> * buttonContainer = new <a href="qhboxlayout.html">QHBoxLayout</a>(); buttonContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( defaultButton ); buttonContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( sansSerifButton ); buttonContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( italicsButton ); <a name="x1513"></a> int maxButtonHeight = defaultButton-><a href="qwidget.html#height">height</a>(); if ( sansSerifButton-><a href="qwidget.html#height">height</a>() > maxButtonHeight ) maxButtonHeight = sansSerifButton-><a href="qwidget.html#height">height</a>(); if ( italicsButton-><a href="qwidget.html#height">height</a>() > maxButtonHeight ) maxButtonHeight = italicsButton-><a href="qwidget.html#height">height</a>(); <a name="x1515"></a> defaultButton-><a href="qwidget.html#setFixedHeight">setFixedHeight</a>( maxButtonHeight ); sansSerifButton-><a href="qwidget.html#setFixedHeight">setFixedHeight</a>( maxButtonHeight ); italicsButton-><a href="qwidget.html#setFixedHeight">setFixedHeight</a>( maxButtonHeight ); <a href="qvboxlayout.html">QVBoxLayout</a> * container = new <a href="qvboxlayout.html">QVBoxLayout</a>( this ); <a name="x1491"></a> container-><a href="qboxlayout.html#addLayout">addLayout</a>( textViewContainer ); container-><a href="qboxlayout.html#addLayout">addLayout</a>( buttonContainer ); <a href="qwidget.html#resize">resize</a>( 700, 250 ); } </pre> <p> <hr> <p> main() program (simple-qfont-demo.cpp): <p> <pre>/* $Id$ */ #include "viewer.h" #include <<a href="qapplication-h.html">qapplication.h</a>> int main( int argc, char **argv ) { <a href="qapplication.html">QApplication</a> app( argc, argv ); Viewer * textViewer = new Viewer(); textViewer-><a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Simple QFont Demo" ); app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( textViewer ); textViewer-><a href="qwidget.html#show">show</a>(); return app.<a href="qapplication.html#exec">exec</a>(); } </pre> <p> <p>See also <a href="qfont-examples.html">QFont Examples</a>. <!-- eof --> <p><address><hr><div align=center> <table width=100% cellspacing=0 border=0><tr> <td>Copyright © 2001 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a> <td align=right><div align=right>Qt version 3.0.2</div> </table></div></address></body> </html>