?? simple-qfont-demo-walkthrough.html
字號:
{ <a href="qfont.html">QFont</a> font( "Newyork", 18 );</pre><p> The slot to change the greeting font to sans serifis quite similar to <a href="#setDefault()">setDefault().</a>Here we save a line of code and define the (non-existant) font family (NewYork)and size (18 points) at once.<p> <pre> <a name="x2116"></a> font.<a href="qfont.html#setStyleHint">setStyleHint</a>( QFont::SansSerif );</pre><p> We use a style hint to ask <a href="qfont.html">QFont</a> for a sans serif font(<em>SansSerif</em> is a member of the QFont::StyleHint enumeration). <p> As a member of the NewYork family is quite unlikely to be installedon your computer, QFont will try to follow the style hint and the font sizeand use this information to find a replacement font.<p> <pre> greetings-><a href="qwidget.html#setFont">setFont</a>( font );</pre><p> Finally we apply the requested font to the content of the<em>greetings</em> textview ...<p> <pre> showFontInfo( font ); }</pre><p> ... and display the appropriate font information in the <em>fontInfo</em> textview. <p> <a name="setItalics()"></a><p> <pre> void 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="x2114"></a> font.<a href="qfont.html#setItalic">setItalic</a>( TRUE );</pre><p> The <a href="#setItalics()">setItalics()</a> slot changes the greetings'font to a 32 pt bold and italic member of the (again non-existant)Tokyo family. Note that <a href="#setFontSubstitutions()">setFontSubstitutions()</a> defines a substitution family for Tokyo. <p> <pre> greetings-><a href="qwidget.html#setFont">setFont</a>( font );</pre><p> We set the font of the <em>greetings</em> textview to <em>font</em>, and ...<p> <pre> showFontInfo( font ); }</pre><p> ... display the appropriate font information in the <em>fontInfo</em> textview.<p> <a name="showFontInfo()"></a><p> <pre> void Viewer::showFontInfo( <a href="qfont.html">QFont</a> & font ) {</pre><p> Now, how do we show the font information?<p> <pre> <a href="qfontinfo.html">QFontInfo</a> info( font );</pre><p> First we obtain information about the font that is actually used when the font <em>font</em> is required, and store it in <em>info</em>.<p> <pre> <a href="qstring.html">QString</a> messageText; messageText = "Font requested: \"" + font.<a href="qfont.html#family">family</a>() + "\" " +</pre><p> Then we start compiling the message that we want to show in the <em>fontInfo</em>textview.First, we print out the <em>requested</em> font family name. As we want toframe the family name with quotation marks, we have to escape the <em>"</em>character so that it is not confused with the C++ quotation marksused to terminate strings.<p> <pre> <a name="x2123"></a><a name="x2113"></a> QString::<a href="qstring.html#number">number</a>( font.<a href="qfont.html#pointSize">pointSize</a>() ) + "pt<BR>" +</pre><p> We obtain the <em>requested</em> font size in points and convert it toa <a href="qstring.html">QString</a> using <a href="qstring.html#number">QString::number</a>(). Using <em><BR></em>; we add a rich-textlinebreak to the <em>messageText</em> string.<p> <pre> "Font used: \"" +</pre><p> After we have displayed information about the <em>required</em> fontwe want to contrast it with the one <em>actually</em> used.This is stored in the <a href="qfontinfo.html">QFontInfo</a> <em>info</em> variable. <p> <pre> <a name="x2120"></a> info.<a href="qfontinfo.html#family">family</a>() + "\" " +</pre><p> First we display the font family, ...<p> <pre> <a name="x2121"></a> QString::<a href="qstring.html#number">number</a>( info.<a href="qfontinfo.html#pointSize">pointSize</a>() ) + "pt<P>";</pre><p> ... and then we append the actual font size, converted to a QString, to the message string. The unit abbreviation and a rich-textparagraph (<em><P></em>) follow.<p> If custom substitutions are available for the requested <em>font</em>,we're going to show them as well:<p> <a name="showFontInfo()_QStringList"></a><pre> <a name="x2119"></a><a name="x2110"></a> <a href="qstringlist.html">QStringList</a> substitutions = QFont::<a href="qfont.html#substitutes">substitutes</a>( font.<a href="qfont.html#family">family</a>() );</pre><p> First we store the entire list of substitutes in a string list.<p> <pre> <a name="x2128"></a> if ( ! substitutions.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ){</pre><p> If it contains at least one substitute ...<p> <pre> messageText += "The following substitutions exist for " + \ font.<a href="qfont.html#family">family</a>() + ":<UL>";</pre><p> ... we say so in the <em>messageText</em>, ...<p> <pre> <a name="x2126"></a> QStringList::Iterator i = substitutions.<a href="qvaluelist.html#begin">begin</a>();</pre><p> ... and prepare ourselves to step through the list. For thispurpose we set the list iterator <em>i</em> to the first list memberof the <em>substitutions</em> string list.<p> <pre> <a name="x2127"></a> while ( i != substitutions.<a href="qvaluelist.html#end">end</a>() ){</pre><p> As long as we haven't reached the last list member ...<p> <pre> messageText += "<LI>\"" + (* i) + "\"";</pre><p> we add a bullet list entry (<em><LI></em>)of the current list member (i.e. the font family nameof the substitute), ...<p> <pre> i++; }</pre><p> ... and move the iterator one step further.<a name="setFontSubstitutions()"></a><p> <pre> messageText += "</UL>";</pre><p> Finally we add the end-of-bullet-list rich-text tag to the <em>messageText</em>string.<p> <pre> } else { messageText += "No substitutions exist for " + \ font.<a href="qfont.html#family">family</a>() + "."; }</pre><p> If the substitution list was empty, we make a note aboutit in the <em>messageText</em> string.<p> <pre> fontInfo-><a href="qtextedit.html#setText">setText</a>( messageText ); }</pre><p> Now that we have the <em>messageText</em> string ready we enter it intothe <em>fontInfo</em> textview.<p> <pre> void Viewer::setFontSubstitutions() {</pre><p> With this function we finally reveal the secret of how todefine custom substitutions for a font family.<p> <pre> <a href="qstringlist.html">QStringList</a> substitutes;</pre><p> All we need is a string list.<p> <pre> <a name="x2125"></a> substitutes.<a href="qvaluelist.html#append">append</a>( "Times" ); substitutes += "Mincho", substitutes << "Arabic Newspaper" << "crox";</pre><p> In a real world application you will probably stick to one of the abovemethods to add strings to a string list. Here all possible ones are outlinedto give you an overview. <p> After these append operations <em>substitutes</em> consists of four members:<em>Times</em>, <em>Mincho</em>, <em>Arabic Newspaper</em>, and <em>Crox</em> in thisorder. These are the font families that in the first place are searched for characters the base font does not provide.<p> <pre> <a name="x2112"></a> QFont::<a href="qfont.html#insertSubstitutions">insertSubstitutions</a>( "Bavaria", substitutes );</pre><p> In <em>Viewer</em> objects, these four families provide a fallbackfor the Bavaria font family requested by the <a href="#setDefault()">setDefault()</a> slot.<p> <pre> <a name="x2111"></a> QFont::<a href="qfont.html#insertSubstitution">insertSubstitution</a>( "Tokyo", "Lucida" ); }</pre><p> For the Tokyo family used in <a href="#setItalics()">setItalics()</a>we provide only one substitute family, Lucida. Because it isonly one and not many as for Bavaria, we use <a href="qfont.html#insertSubstitution">QFont::insertSubstitution</a>() instead of <a href="qfont.html#insertSubstitutions">QFont::insertSubstitutions</a>().<p> If you usually create your GUIs using Qt Designer this walkthroughhas already come to an end. If this is one of your first encounterswith Qt you might however continue with the explanationof the very simple <a href="#simple-qfont-demo.cpp">main() function.</a><p> <a name="layout()"></a><p> <pre> <a name="x2130"></a>void Viewer::<a href="qwidget.html#layout">layout</a>() {</pre><p> This last member function of the <em>Viewer</em> class does not coverany more <a href="qfont.html">QFont</a> details. All it does is creating a nice automaticlayout for the three push buttons and the two text views.<p> The best solution for this task is to have the two <a href="qtextview.html">QTextView</a>slined up horizontally. The same applies to the <a href="qpushbutton.html">QPushButton</a>s.Finally both of these layouts are placed together into a verticallayout container. Qt takes care of the proportions so that everythinglooks nice.<p> <pre> <a href="qhboxlayout.html">QHBoxLayout</a> * textViewContainer = new <a href="qhboxlayout.html">QHBoxLayout</a>();</pre><p> Let's create the first layout that aligns its members horizontally, ...<p> <pre> <a name="x2108"></a> textViewContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( greetings );</pre><p> ... and add the QTextView with the greetings, ...<p> <pre> textViewContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( fontInfo );</pre><p> ... as well as the text view with the font information. <em>fontInfo</em> appearsto the right of <em>greetings</em> because it was added later.<p> <pre> <a href="qhboxlayout.html">QHBoxLayout</a> * buttonContainer = new <a href="qhboxlayout.html">QHBoxLayout</a>();</pre><p> Now we create the second layout for the push buttons.<p> <pre> buttonContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( defaultButton );</pre><p> <em>defaultButton</em> is placed on the left hand side of the layout, ...<p> <pre> buttonContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( sansSerifButton );</pre><p> ... <em>sansSerifButton</em> in the middle, ...<p> <pre> buttonContainer-><a href="qboxlayout.html#addWidget">addWidget</a>( italicsButton );</pre><p> ... and <em>italicsButton</em> on the right hand side.<p> Unfortunately we face a tiny problem: remember that (a highly unusualthing to do in a real world application) the labels of the threebuttons are drawn in different fonts. Whilst the automatic layoutaccounts for the fact all three buttons have the same width, the uncommonoccurrence of different character heights leads to different button heights.<p> To make the application window look nice we have to help it a little.<p> <pre> <a name="x2129"></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>();</pre><p> By comparing the three button heights we find the largest oneand store it in <em>maxButtonHeight</em>.<p> <pre> <a name="x2131"></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 );</pre><p> Now we set the height of each button to this maximum value and make surethat the automatic layout does not change it. <p> This was the hardest part of the entire layout process. There is one taskleft:<p> <pre> <a href="qvboxlayout.html">QVBoxLayout</a> * container = new <a href="qvboxlayout.html">QVBoxLayout</a>( this );</pre><p> We create a layout that arranges its members vertically.<p> <pre> <a name="x2107"></a> container-><a href="qboxlayout.html#addLayout">addLayout</a>( textViewContainer );</pre><p> This <em>container</em> layout contains the text views on top, ...<p> <pre> container-><a href="qboxlayout.html#addLayout">addLayout</a>( buttonContainer );</pre><p> ... and the button row below.<p> <pre> <a href="qwidget.html#resize">resize</a>( 700, 250 ); }</pre><p> Finally we set the size of the entire main window to a width of 700 pixelsand a height of 250 pixels.<p> <h3><a name="simple-qfont-demo.cpp">The main program</a></h3><p> There is not much to say about the main program.<p> <pre> #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();</pre><p> We create an instance of the <a href="#viewer.cpp">Viewer</a>class, ...<p> <pre> <a name="x2134"></a> app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( textViewer );</pre><p> ... make it the main widget of the application object <em>app</em>, ...<p> <pre> <a name="x2135"></a> textViewer-><a href="qwidget.html#show">show</a>();</pre><p> ... display it to the user ...<p> <pre> <a name="x2133"></a> return app.<a href="qapplication.html#exec">exec</a>(); }</pre><p> ... and enter the application loop. Well done, that was all for today ...<p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <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.5</div></table></div></address></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -