📄 simple-qfont-demo-walkthrough.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/doc/simple-qfont-demo-walkthrough.doc:5 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Walkthrough: A simple QFont demonstration</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>Walkthrough: A simple QFont demonstration</h1> <p> <p> The following walkthroughwill show you how to make use of the font setting and manipulation techniques provided by <a href="qfont.html">QFont</a>.<p> In addition it will show some aspects of widget layout -- if you prefer leavingthis job to Qt Designer simply skip the <a href="#layout()">relevant paragraphs.</a> Moreover, if youhave asked yourself how to add strings to a <a href="qstringlist.html">QStringList</a> and how tostep through its members, you will know after reading this walkthrough,or skipping to the relevant explanations in <a href="#showFontInfo()_QStringList">Viewer::showFontInfo()</a> and <a href="#setFontSubstitutions()">Viewer::setFontSubstitutions().</a><p> To get the most out of the walkthrough you should at least be familiar with<a href="signalsandslots.html">signals and slots.</a><p> The <a href="simple-font-demo-example.html">example program</a> consists of a widget containing two <a href="qtextview.html">QTextView</a>s side by side.The one on the left shows greetings in English, Russian, and Hebrew.The one on the right shows some information about the fonts used todraw these greetings. Three push buttons in the bottom of the main windowchange the font used to display the greetings.<p> Note that the fonts and font characteristics in the example have been chosenfor demonstration purposes only -- in a real world application theywould rather count for bad design. <p> <h3><a name="viewer.h">The API of the custom widget</a></h3><p> The widget used in this example is a custom widget named<em>Viewer</em>.<p> <pre> #include <<a href="qwidget-h.html">qwidget.h</a>> #include <<a href="qfont-h.html">qfont.h</a>></pre><p> As we derive it from <a href="qwidget.html">QWidget</a> we include the relevant header file.Additionally we use a <a href="qfont.html">QFont</a> object as a function argument, andtherefore include the QFont class.<p> <pre> class QTextView; class QPushButton;</pre><p> Furthermore we declare the use of the QTextView and the<a href="qpushbutton.html">QPushButton</a> classes for class variables (we don't need toinclude them at this stage yet because we only use pointers to these classes).<p> <pre> class Viewer : public <a href="qwidget.html">QWidget</a> { <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a></pre><p> The <em>Viewer</em> widget will have slots, so don't forget to add the<em>Q_OBJECT</em> macro.<p> <pre> public: Viewer();</pre><p> As we won't use more than one instance of this class there is no need for any complex constructors, a simple one withoutany arguments should be sufficient.<p> <pre> private slots: void setDefault();</pre><p> As previously mentioned we're going to have three push buttons. When the userclicks on them, something should happen. Thus we define <a href="#setDefault()">one slot thatsets the font in the greeting window back to the default,</a> ...<p> <pre> void setSansSerif();</pre><p> ... <a href="#setSansSerif()">one that switches to a sans serif font,</a>and ...<p> <pre> void setItalics();</pre><p> ... <a href="#setItalics()">one that shows the greetings in italics.</a><p> <pre> private: void setFontSubstitutions();</pre><p> Will will write the greetings using different alphabets. For users who don'thave Unicode fonts installed we want to tell the application to tryto exchange missing characters in one font with appropriate charactersfrom other fonts. <a href="qfont.html">QFont</a> does such font substitutions on its own butwith <a href="#setFontSubstitutions()">this helper function</a>we can define our preferred substitution pattern.<p> <pre> void layout();</pre><p> The task of putting the buttons and text views together we will put into a separate <a href="#layout()">layout()</a> function. This will make the code easier to understand and read.<p> <pre> void showFontInfo( <a href="qfont.html">QFont</a> & );</pre><p> <a href="#showFontInfo()">The last private function</a> reveals font information in the text view on the right.<p> <pre> <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; };</pre><p> Last but not least we define the elements of our GUI as privateclass variables.<p> <h3><a name="viewer.cpp">The implementation of the Viewer widget</a></h3><p> Now we will implement the <em>Viewer</em> class.<p> <pre> #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>></pre><p> First we include the relevant header files -- obviously the <a href="#viewer.h">header of the Viewer class</a> itself,of the <a href="qpushbutton.html">QPushButton</a> and QTextView widgets, and of the <a href="qstring.html">QString</a>and <a href="qstringlist.html">QStringList</a> classes. <em>qlayout.h</em> providesclasses for horizontal and vertical layout and will be usedin the <a href="#layout()">layout()</a> function.<p> <a name="Viewer()"></a><p> <pre> Viewer::Viewer() :<a href="qwidget.html">QWidget</a>() {</pre><p> As already mentioned the finger print of the <em>Viewer</em> constructoris as simple as possible, without any arguments, derived fromthe <a href="qwidget.html">QWidget</a> default constructor.<p> <pre> setFontSubstitutions();</pre><p> First we define the font substitutions -- for clarity reasons wedo this in a <a href="#setFontSubstitutions()">separate function.</a><p> <pre> <a name="x2122"></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" );</pre><p> The Hebrew and the Russian greeting we have readily available as UTF8 encodedstrings.To use them in a <a href="qstring.html">QString</a> we "import" them with <a href="qstring.html#fromUtf8">QString::fromUtf8</a>(). <p> <pre> <a href="qstring.html">QString</a> greeting_en( "Hello" );</pre><p> Dor the English greeting we use a simple QString. <p> <pre> greetings = new <a href="qtextview.html">QTextView</a>( this, "textview" );</pre><p> Now we create the first widget as a child of <em>this</em> widget, the QTextView with the identity name <em>textview</em> that shows the greetings. <p> <pre> greetings-><a href="qtextedit.html#setText">setText</a>( greeting_en + "\n" + greeting_ru + "\n" + greeting_heb );</pre><p> Now we set the text shown by <em>greetings</em> to the three greetings.<p> <pre> fontInfo = new <a href="qtextview.html">QTextView</a>( this, "fontinfo" );</pre><p> The second text view we call <em>fontinfo</em> and createit as a child of <em>this</em> <em>Viewer</em> widget.<p> <pre> setDefault();</pre><p> Using the <a href="#setDefault()">setDefault()</a> function weapply the initial font to the greetings <em>greetings</em> and fill the <em>fontInfo</em> textview with information about the font used.<p> <pre> defaultButton = new <a href="qpushbutton.html">QPushButton</a>( "Default", this, "pushbutton1" );</pre><p> Now we create the first of the three push buttons -- the onethat changes the font to the initial one -- with the label <em>Default</em>.<p> <pre> <a name="x2132"></a> defaultButton-><a href="qwidget.html#setFont">setFont</a>( QFont( "times" ) );</pre><p> The label should be printed in a member font of the Times family.In the unlikely case that the user does not have installed amatching font, <a href="qfont.html">QFont</a> is responsible in finding a replacement.Note that case-sensitivity is no issue when specifying the font family.<p> As we don't explicitly request a font size or weight, QFont tries tofind a default 12 pt font with normal boldness. <p> <pre> <a name="x2109"></a> <a href="qobject.html#connect">connect</a>( defaultButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( setDefault() ) );</pre><p> In order to make something happening when the user clicks the<em>defaultButton</em>, we connect the <a href="qbutton.html#clicked">QPushButton::clicked</a>() signalsissued from it to the <em>Viewer</em>'s <a href="#setDefault()">setDefault()</a>slot.<p> <pre> sansSerifButton = new <a href="qpushbutton.html">QPushButton</a>( "Sans Serif", this, "pushbutton2" ); sansSerifButton-><a href="qwidget.html#setFont">setFont</a>( QFont( "Helvetica", 12 ) );</pre><p> The newly created second button is labelled <em>Sans Serif</em>in a 12 pt Helvetica font. Again if this is not possible becausethe requested font is not available on the system, <a href="qfont.html">QFont</a> dealswith it and finds a replacement.<p> <pre> <a href="qobject.html#connect">connect</a>( sansSerifButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( setSansSerif() ) );</pre><p> We connect the clicked() signal of the <em>sansSerifButton</em> to the <a href="#setSansSerif()">setSansSerif()</a> slot.<p> <pre> 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 ) );</pre><p> <em>italicsButton</em>, the last push button, is labelled <em>Italics</em>.This time we specify even more characteristics of thelabel font. We wish it to be a 12 pt bold member of the Lucida family.Also it should be in italics, indicated by the fourth QFont argumentbeing TRUE.<p> <pre> <a href="qobject.html#connect">connect</a>( italicsButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( setItalics() ) );</pre><p> Again, the <em>italicsButton</em>'s clicked() signal is connected toa slot of <em>this</em> <em>Viewer</em> object <a href="#setItalics()">setItalics().</a><p> <pre> <a href="qwidget.html#layout">layout</a>(); }</pre><p> Finally we arrange all five child widgets of <em>this</em> main windownicely using <a href="#layout()">layout().</a><p> <a name="setDefault()"></a><p> <pre> void Viewer::setDefault() { <a href="qfont.html">QFont</a> font( "Bavaria" );</pre><p> For demonstration purposes on how the <a href="qfont.html">QFont</a> substitution workswe use a non-existant font family, Bavaria, as the default font forthe greetings.<p> <pre> <a name="x2115"></a> font.<a href="qfont.html#setPointSize">setPointSize</a>( 24 );</pre><p> This font should have a size of 24 points, ...<p> <pre> <a name="x2118"></a> font.<a href="qfont.html#setWeight">setWeight</a>( QFont::Bold );</pre><p> ... it should be bold, ...<p> <pre> <a name="x2117"></a> font.<a href="qfont.html#setUnderline">setUnderline</a>( TRUE );</pre><p> ... and the text written should be underlined.<p> <pre> greetings-><a href="qwidget.html#setFont">setFont</a>( font );</pre><p> Now we ask the <em>greetings</em> widget to use the font <em>font</em>.<p> As a member of the Bavaria font family is unlikely tobe installed on your machine, run the program and observehow QFont finds a substitute. Later on we will definecustom substitutions for Bavaria in the <a href="#setFontSubstitutions()">setFontSubstitutions()</a> function.<p> <pre> showFontInfo( font ); }</pre><p> Finally we use the function <a href="#showFontInfo()">showFontInfo()</a> to display appropriate informationabout the current font and how it maybe differs from the one requested.<p> <a name="setSansSerif()"></a><p> <pre> void Viewer::setSansSerif()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -