simple-application.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 618 行 · 第 1/3 页
HTML
618 行
<a href="qtextstream.html">QTextStream</a> t( &f ); t << text; f.<a href="qfile.html#close">close</a>();</pre><p> As its name suggests, this function saves the current file. If no filename has beenspecified so far, the <A HREF="#saveAs()"><em>saveAs()</A></em> routine is called. Unwritable files cause the <em>ApplicationWindow</em> object to providean error-message in the statusbar. Note that there are more thanone possibilities to achieve this: compare the above <em>statusBar()->message()</em> line with the appropriate code in the <em>load()</em> function.<p> <pre> e-><a href="qtextedit.html#setModified">setModified</a>( FALSE );</pre><p> Tell the editor that the contents haven't been edited since the lastsave. When the user does some further editing and wishes to close the window without explicit saving, <A HREF="#closeEvent"><em>ApplicationWindow::closeEvent()</A></em> will ask about it.<p> <pre> <a href="qwidget.html#setCaption">setCaption</a>( filename );</pre><p> It may be that the document was saved under a different name than theold caption suggests, so we set the window caption just to be sure.<p> <pre> <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString( "File %1 saved" ).arg( filename ), 2000 ); }</pre><p> With a message in the statusbar, we inform the user that the filewas saved successfully.<p> <A NAME="saveAs()"></A><pre> void ApplicationWindow::saveAs() { <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) { filename = fn; save(); } else { <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Saving aborted", 2000 ); } }</pre><p> This function asks for a new name, saves the document under that name,and implicitly changes the window system caption to the new name.<p> <a name="printer"></a><p> <pre> void ApplicationWindow::print() {</pre><pre> const int Margin = 10; int pageNo = 1;</pre><p> <em>print()</em> is called by the <em>File->Print</em> menu item and the <em>filePrint</em> tool button.<p> Because we don't want to print to the very edges of the paper, we use alittle margin: 10 points. Furthermore we keep track of the page count.<p> <pre> if ( printer-><a href="qprinter.html#setup">setup</a>(this) ) { // printer dialog</pre><p> <a href="qprinter.html#setup">QPrinter::setup</a>() invokes a print dialog, configures the printerobject, and returns TRUE if the user wants to print or FALSE if not.So we test the return value; if it's TRUE, we...<p> <pre> <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing..." );</pre><p> ... set a statusbar message in case printing takes a while.<p> <pre> <a href="qpainter.html">QPainter</a> p; if( !p.<a href="qpainter.html#begin">begin</a>( printer ) ) // paint on printer return;</pre><p> We create a painter for the output and decide that we wish to painton the printer or do nothing at all.<p> <pre> p.<a href="qpainter.html#setFont">setFont</a>( e-><a href="qtextedit.html#font">font</a>() ); int yPos = 0; // y-position for each line <a href="qfontmetrics.html">QFontMetrics</a> fm = p.<a href="qpainter.html#fontMetrics">fontMetrics</a>(); <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( printer ); // need width/height // of printer surface</pre><p> Then we select the font our <a href="qtextedit.html">QTextEdit</a> object returns as its currentone, and set up some variables we'll need.<p> <pre> for( int i = 0 ; i < e-><a href="qtextedit.html#lines">lines</a>() ; i++ ) {</pre><p> As long as the editing widget contains more lines, we want to print them.<p> <pre> if ( Margin + yPos > metrics.<a href="qpaintdevicemetrics.html#height">height</a>() - Margin ) {</pre><p> Before we print a line, we make sure that there is space for it on the current page. If not, we start a new page:<p> <pre> <a href="qstring.html">QString</a> msg( "Printing (page " ); msg += QString::<a href="qstring.html#number">number</a>( ++pageNo ); msg += ")..."; <a href="qmainwindow.html#statusBar">statusBar</a>()->message( msg ); printer-><a href="qprinter.html#newPage">newPage</a>(); // no more room on this page yPos = 0; // back to top of page</pre><p> (Four lines to tell the user what we're doing, two lines to do it.)<p> <pre> }</pre><p> Now we know that there's space for the current line ...<p> <pre> p.<a href="qpainter.html#drawText">drawText</a>( Margin, Margin + yPos, metrics.<a href="qpaintdevicemetrics.html#width">width</a>(), fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>(), ExpandTabs | DontClip, e-><a href="qtextedit.html#text">text</a>( i ) );</pre><p> ... and we use the painter to print it. <p> In Qt, output to printers uses the exact same code as output to screen,pixmaps and picture metafiles. Therefore, we don't call a <a href="qprinter.html">QPrinter</a>function to draw text, but a <a href="qpainter.html">QPainter</a> function. QPainter works on all theoutput devices mentioned and has a device independent API. Most of itscode is device independent, too, therefore it is less likely that yourapplication will have odd bugs. (If the same code is used to print and todraw on the screen, it's less likely that you'll have print-only orscreen-only bugs.)<p> <pre> yPos = yPos + fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>();</pre><p> With this line, we keep count of how much of the paper we've usedso far.<p> <pre> } p.<a href="qpainter.html#end">end</a>(); // send job to printer</pre><p> At this point we've printed all of the text in the editing widget and told the printer to finish off the last page. <p> <pre> <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing completed", 2000 );</pre><p> Finally the user receives the message that we're done.<p> <pre> } else { <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing aborted", 2000 ); }</pre><p> If the user did not want to print (and <a href="qprinter.html#setup">QPrinter::setup</a>() returnedFALSE), we inform him or her about it.<p> <pre> }</pre><p> With this little effort we have printed a text document.So let's care about what happens when a user wishes to <em>close()</em>an <em>ApplicationWindow</em>.<p> <a name="closeEvent"></a><pre> void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a>* ce ) {</pre><p> This event gets to process window system close events. A close event issubtly different from a hide event: hide often means "iconify" whereasclose means that the window is going away for good.<p> <pre> if ( !e-><a href="qtextedit.html#isModified">isModified</a>() ) { ce-><a href="qcloseevent.html#accept">accept</a>(); return; }</pre><p> If the text hasn't been edited, we just accept the event. The windowwill be closed, and because we used the <em>WDestructiveClose</em> <a href="qt.html#WidgetFlags">widget flag</a> in the <a href="#ApplicationWindow">\e ApplicationWindow() constructor</a>, the widget will be deleted.<p> <pre> switch( QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Qt Application Example", "Do you want to save the changes" " to the document?", "Yes", "No", "Cancel", 0, 1 ) ) {</pre><p> Otherwise we ask the user: What do you wantto do? <p> <pre> case 0: save(); ce-><a href="qcloseevent.html#accept">accept</a>(); break;</pre><p> If he/she wants to save and then exit, we do that. <p> <pre> case 1: ce-><a href="qcloseevent.html#accept">accept</a>(); break;</pre><p> If the user however doesn't want to exit, we ignore the close event (thereis a chance that we can't block it but we try).<p> <pre> case 2: default: // just for sanity ce-><a href="qcloseevent.html#ignore">ignore</a>(); break;</pre><p> The last case -- the user wants to abandon the edits and exit -- is verysimple.<p> <pre> } }</pre><p> Last but not least we implement the slots used by the help menu entries.<p> <pre> void ApplicationWindow::about() { QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Qt Application Example", "This example demonstrates simple use of " "QMainWindow,\nQMenuBar and QToolBar."); } void ApplicationWindow::aboutQt() { QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Application Example" ); }</pre><p> These two slots use ready-made "about" functions to provide someinformation about this program and the GUI toolkit it uses. (Although youdon't need to provide an About Qt in your programs, if you use Qt for freewe would appreciate it if you tell people what you're using.)<p> That was all we needed to write a complete, almost useful application withnice help-functions, almost as good as the "editors" some computer vendorsship with their desktops, in less than 300 lines of code. As we promised -a simple application.<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 + =
减小字号Ctrl + -
显示快捷键?