designer-manual-3.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 337 行 · 第 1/4 页
HTML
337 行
this, "Rich Edit", "The document contains unsaved changes.\n" "Do you want to save the changes?", "&Save", "&Don't Save", "&Cancel " + action, 0, // Enter == button 0 2 ) ) { // Escape == button 2 case 0: // Save; continue fileSave(); break; case 1: // Do not save; continue break; case 2: // Cancel continueAction = 0; break; } } return continueAction; }</pre> <p>The <tt>saveAndContinue()</tt> function is included for completeness.</p><h5><a name="2-1-2"></a>Aligning Text</h5><pre> void EditorForm::changeAlignment(QAction * align) { if ( align == leftAlignAction ) textEdit->setAlignment( Qt::<a href="qt.html#AlignmentFlags-enum">AlignLeft</a> ); else if ( align == rightAlignAction ) textEdit->setAlignment( Qt::<a href="qt.html#AlignmentFlags-enum">AlignRight</a> ); else if ( align == centerAlignAction ) textEdit->setAlignment( Qt::<a href="qt.html#AlignmentFlags-enum">AlignCenter</a> ); }</pre> <p>We compare the chosen alignment action's pointer to the the pointers stored in the form and if we get a match set the appropriate alignment in the textEdit widget.</p><h5><a name="2-1-3"></a>Changing Fonts</h5><p>We've already connected the fontComboBox's<!-- index activated() --> <tt>activated()</tt> signal to the textEdit's<!-- index setFamily() --> <tt>setFamily()</tt> slot so we just have to populate the combobox with the font names when we call<!-- index init() --> <tt>init()</tt>.</p><pre> void EditorForm::init() { textEdit->setFocus(); <a href="qfontdatabase.html">QFontDatabase</a> fonts; fontComboBox->insertStringList( fonts.<a href="qfontdatabase.html#families">families</a>() ); <a href="qstring.html">QString</a> font = textEdit->family(); <a href="qwidget.html#font-prop">font</a> = font.<a href="qstring.html#lower">lower</a>(); for ( int i = 0 ; i < fontComboBox->count(); i++ ) { if ( <a href="qwidget.html#font-prop">font</a> == fontComboBox->text( i ) ) { fontComboBox->setCurrentItem( i ); break; } } }</pre> <p>The first line sets the focus as we've already mentioned. We then create a <b>QFontDatabase</b> object and insert its list of font families into the fontComboBox. Finally we set the fontComboBox's current item to the textEdit's current font.</p><h4><a name="2-2"></a>Making the Application Run</h4><!-- index Object Hierarchy --><!-- index Forms!Forward declarations --><!-- index Forward declarations --><!-- index Includes --><!-- index Adding!Forward declarations --><!-- index Adding!Includes --><p>With all the connections and code in place we are now ready to make our application run. Click on the Source tab of the Object Hierarchy window and click on the Includes (in Implementation) item. We need to include the files that our source code depends on. Right click the Includes item and click New. Type in <tt><qapplication.h></tt> for <tt>fileExit()</tt>'s<!-- index exit() --> <tt>exit()</tt> call. In the same way add <tt><qmessagebox.h></tt> for <tt>saveAndContinue()</tt>'s message box, <tt><qfiledialog.h></tt> for the <tt>fileOpen()</tt> and <tt>fileSaveAs()</tt> functions, and <tt><qfontdatabase.h></tt> for the <b>QFontDatabase</b> class in<!-- index init() --> <tt>init()</tt>.</p><!-- index Forms!Class variables --><!-- index Adding!Class variables --><!-- index Class variables --><p>We referred to a member variable, <tt>fileName</tt>, in our source code so we must add it to the form. Click the Source tab, right click the Class Variables item, click New from the pop up menu, then enter 'QString fileName;'.</p><!-- index Adding!Source Files to Project Files --><p>The simplest way to create a new source file is by clicking <b>File|New</b> to invoke the 'New File' dialog, then click 'C++ Source' or 'C++ Header' as appropriate, then click <b>OK</b>. A new empty source window will appear. Click <b>File|Save</b> to invoke the <em>Save As</em> dialog, enter 'main.cpp', then click <b>Save</b>. Enter the following code in the <tt>main.cpp</tt> C++ editor window:</p><pre> #include <<a href="qapplication-h.html">qapplication.h</a>> #include "richedit.h" int main( int argc, char *argv[] ) { <a href="qapplication.html">QApplication</a> app( argc, argv ); EditorForm richeditForm; app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &richeditForm ); richeditForm.show(); return app.<a href="qapplication.html#exec">exec</a>(); }</pre><!-- index Makefiles --> <p>All that's left to do is to generate the Makefile, compile and run. The Makefile is created with <tt>qmake</tt>: <tt>qmake -o Makefile richedit.pro</tt>.</p><p>The richedit application demonstrates how easy it is to create a Qt application's main window with menus and dockable toolbars. A great deal of functionality was obtained by connecting the appropriate built-in signals and slots. The remaining functionality was achieved by connecting built-in signals to our own custom slots. We could continue developing the application, for example updating the fontComboBox, the font size spinbox and the actions with the font attributes as the user moves the cursor through their text. But our objective has been to demonstrate the creation of a main window with actions, menus and toolbars so we must stop at this point and leave further development and experimentation to you.</p><!-- eof --><p align="right">[<a href="designer-manual-2.html">Prev: Creating a Qt Application</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-4.html">Next: The Designer Approach</a>]</p><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 + =
减小字号Ctrl + -
显示快捷键?