designer-manual-3.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 337 行 · 第 1/4 页
HTML
337 行
<blockquote><p align="center"><em>Connecting the Alignment Actions</em></p></blockquote><p>We'll have to write the code to set the alignment ourselves; we'll cover this in <a href="designer-manual-3.html#2-1-2">Aligning Text</a>.</p><h5><a name="1-5-4"></a>Connecting for Font Names and Sizes</h5><!-- index Connecting!Signals and Slots --><!-- index Signals and Slots!Connecting for Font Sizes --><!-- index Signals and Slots!Connecting for Font Names --><!-- index Font Sizes --><!-- index Previewing!Signals and Slots --><!-- index Signals and Slots!Previewing --><p>We'll start by dealing with font size since it's easiest. Click the <b>Connect Signals/Slots</b> toolbar button then click the spinbox and drag to the text edit widget; release on the text edit. Click the<!-- index valueChanged(int) \SignalOrSlot valueChanged(int) --> signal and then click the textEdit's<!-- index setPointSize(int) \SignalOrSlot --> setPointSize(int) slot. Click <b>OK</b> and font sizes are done. (Since font sizes are handled purely through built-in signals and slots they work in preview mode.)</p><p align="center"><img align="middle" src="qd-chapmainwin-connfontsize.png" width="617" height="363"></p><blockquote><p align="center"><em>Connecting the Font Sizes</em></p></blockquote><p>Connect the fontComboBox's<!-- index activated() --> <tt>activated()</tt> signal to the textEdit's<!-- index setFamily() --> <tt>setFamily()</tt> slot. This connection will handle updating the textEdit's font family with the user's choice of font. Note that when you invoke the <em>Edit Connections</em> dialog the first signal that is highlighted is <tt>activated(int)</tt>. Since the<!-- index --> setFamily() <tt>setFamily()</tt> slot takes a <b>QString</b> argument it does <em>not</em> appear in the list of slots. Only those slots which are compatible with the highlighted signal are shown, in this case, slots which take no argument or which take an integer argument. Click the <tt>activated(const QString</tt>&) signal and the list of slots will change to those which take no argument or which take a <b>QString</b> argument; the list will now include<!-- index setFamily() --> <tt>setFamily()</tt> since this takes a <b>QString</b> argument. We will have to populate the combobox with the font names for the user to choose from in code. (See the<!-- index init() \Func init() function --> in <a href="designer-manual-3.html#2-1-3">Changing Fonts</a>.) It's a good idea to connect the fontComboBox's<!-- index activate() \SignalOrSlot activate() signal to --> the textEdit's<!-- index setFocus() \SignalOrSlot setFocus() slot; this --> will ensure that after the user has changed font the focus will return to the text.</p><p>The richedit application is nearly complete. We will have to write code to handle text alignment, font family and file loading and saving. We will also write the code for application exit to deal correctly with any unsaved changes.</p><h3><a name="2"></a>Converting the Design into an Executable Application</h3><!-- index Code Editing --><!-- index Adding!Code!Code Editing --><p>We've built the user interface through <em>Qt Designer</em> and connected those slots that provided sufficient default functionality. The last steps are to code the slots that require customization and then to create<!-- index main.cpp --> <tt>main.cpp</tt> so that we can compile and build our application.</p><h4><a name="2-1"></a>Implementing the Main Window's Functionality</h4><p>When the user starts the richedit application we want the focus to be in the textEdit widget so we need to create an<!-- index init() --> <tt>init()</tt> function with one line of code to achieve this. (All the code snippets are from <tt>qt/tools/designer/examples/richedit/richedit.ui.h</tt>.)</p><pre> void EditorForm::init() { textEdit->setFocus(); }</pre><p>We'll add more to this function later.</p><h5><a name="2-1-1"></a>New Files and Loading and Saving Existing Files</h5><p>The code for these tasks is straightforward. When the user clicks <b>File|New</b> we check to see if there are unsaved changes in the existing text and give them the opportunity to save, continue without saving or cancel the operation. When the user opts to open an existing file or exit the application we perform the same check and offer them the same choices.</p><pre> void EditorForm::fileNew() { if ( saveAndContinue( "New" ) ) textEdit->clear(); }</pre> <p>The <tt>fileNew()</tt> function clears the text and the filename.</p><pre> void EditorForm::fileOpen() { if ( saveAndContinue( "Open" ) ) { <a href="qstring.html">QString</a> fn( QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, "Rich Text Files (*.htm*)", this ) ); if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) { fileName = fn; <a href="qfile.html">QFile</a> file( fileName ); if ( file.<a href="qfile.html#open">open</a>( IO_ReadOnly ) ) { <a href="qtextstream.html">QTextStream</a> ts( &file ); textEdit->setText( ts.<a href="qtextstream.html#read">read</a>() ); } } } }</pre> <p>The <tt>fileOpen()</tt> function asks the user to choose a file using<!-- index QFileDialog::getOpenFileName() --> <tt>QFileDialog::getOpenFileName()</tt>. If they choose a file we set the fileName member to its name, open it and read its contents directly into the text edit via a text stream.</p><pre> void EditorForm::fileSave() { if ( fileName.isEmpty() ) { fileSaveAs(); } else { <a href="qfile.html">QFile</a> f( fileName ); if ( f.<a href="qfile.html#open">open</a>( IO_WriteOnly ) ) { <a href="qtextstream.html">QTextStream</a> ts( &f ); ts << textEdit->text(); textEdit->setModified( FALSE ); } } }</pre> <p>If there is no current file name we call <tt>fileSaveAs()</tt> which will prompt for a file name and if a file name is given calls <tt>fileSave()</tt>. If we have a file name we open a file and write the text from the text edit into the file via a text stream. We also set the text edit's modified property to FALSE.</p><pre> void EditorForm::fileSaveAs() { <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( "", "Rich Text Files (*.htm*)", this ); if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) { fileName = fn; fileSave(); } }</pre> <p>The <tt>fileSaveAs</tt> function prompts the user for a file name and if they give a file name, saves the text to the file by calling <tt>fileSave()</tt>.</p><pre> void EditorForm::fileExit() { if ( saveAndContinue( "Exit" ) ) qApp-><a href="qapplication.html#exit">exit</a>(); }</pre> <p>When we exit the application we must perform the same check for unsaved changes as we've done in the preceding functions, so we've included the <tt>fileExit()</tt> function's code here.</p><pre> int EditorForm::saveAndContinue(const <a href="qstring.html">QString</a> & action) { int continueAction = 1; if ( textEdit->isModified() ) { switch( QMessageBox::<a href="qmessagebox.html#information">information</a>(
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?