📄 application-main-cpp.html
字号:
help-><a href="qmenudata.html#deddb9">insertItem</a>( "What's &This", this, SLOT(<a href="qmainwindow.html#f50ce2">whatsThis</a>()), SHIFT+Key_F1 ); e = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this, "editor" ); e->setFocus(); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>( e ); <a href="qmainwindow.html#530937">statusBar</a>()->message( "Ready", 2000 ); <a href="qwidget.html#ff9d07">resize</a>( 450, 600 );}ApplicationWindow::~ApplicationWindow(){ delete printer;}void <a name="431"></a>ApplicationWindow::newDoc(){ ApplicationWindow *ed = new ApplicationWindow; ed-><a href="qwidget.html#d6a291">setCaption</a>("Qt Example - Application"); ed-><a href="qmainwindow.html#eb53e3">show</a>();}void <a name="433"></a>ApplicationWindow::load(){ <a href="qstring.html">QString</a> fn = QFileDialog::getOpenFileName( QString::null, QString::null, this); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) <a href=#433>load</a>( fn ); else <a href="qmainwindow.html#530937">statusBar</a>()->message( "Loading aborted", 2000 );}void <a name="433"></a>ApplicationWindow::load( const char *fileName ){ <a href="qfile.html">QFile</a> f( fileName ); if ( !f.<a href="qfile.html#255995">open</a>( IO_ReadOnly ) ) return; e->setAutoUpdate( FALSE ); e->clear(); <a href="qtextstream.html">QTextStream</a> t(&f); while ( !t.<a href="qtextstream.html#bb145b">eof</a>() ) { <a href="qstring.html">QString</a> s = t.<a href="qtextstream.html#ae4af4">readLine</a>(); e->append( s ); } f.<a href="qfile.html#64e640">close</a>(); e->setAutoUpdate( TRUE ); e->repaint(); e->setEdited( FALSE ); <a href="qwidget.html#d6a291">setCaption</a>( fileName ); <a href="qstring.html">QString</a> s; s.<a href="qstring.html#926f67">sprintf</a>( "Loaded document %s", fileName ); <a href="qmainwindow.html#530937">statusBar</a>()->message( s, 2000 );}void <a name="434"></a>ApplicationWindow::save(){ if ( filename.isEmpty() ) { <a href=#435>saveAs</a>(); return; } <a href="qstring.html">QString</a> text = e->text(); <a href="qfile.html">QFile</a> f( filename ); if ( !f.<a href="qfile.html#255995">open</a>( IO_WriteOnly ) ) { <a href="qmainwindow.html#530937">statusBar</a>()->message( <a href="qstring.html">QString</a>("Could not write to %1").arg(filename), 2000 ); return; } <a href="qtextstream.html">QTextStream</a> t( &f ); t << text; f.<a href="qfile.html#64e640">close</a>(); e->setEdited( FALSE ); <a href="qwidget.html#d6a291">setCaption</a>( filename ); <a href="qmainwindow.html#530937">statusBar</a>()->message( <a href="qstring.html">QString</a>( "File %1 saved" ).arg( filename ), 2000 );}void <a name="435"></a>ApplicationWindow::saveAs(){ <a href="qstring.html">QString</a> fn = QFileDialog::getSaveFileName( QString::null, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) { filename = fn; <a href=#434>save</a>(); } else { <a href="qmainwindow.html#530937">statusBar</a>()->message( "Saving aborted", 2000 ); }}void <a name="436"></a>ApplicationWindow::print(){ const int Margin = 10; int pageNo = 1; if ( printer->setup(this) ) { // printer dialog <a href="qmainwindow.html#530937">statusBar</a>()->message( "Printing..." ); <a href="qpainter.html">QPainter</a> p; if( !p.<a href="qpainter.html#02ed5d">begin</a>( printer ) ) return; // paint on printer p.<a href="qpainter.html#998df2">setFont</a>( e->font() ); int yPos = 0; // y position for each line <a href="qfontmetrics.html">QFontMetrics</a> fm = p.<a href="qpainter.html#73b2e5">fontMetrics</a>(); <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( printer ); // need width/height // of printer surface for( int i = 0 ; i < e->numLines() ; i++ ) { if ( Margin + yPos > metrics.<a href="qpaintdevicemetrics.html#d4fedb">height</a>() - Margin ) { <a href="qstring.html">QString</a> msg( "Printing (page " ); msg += QString::number( ++pageNo ); msg += ")..."; <a href="qmainwindow.html#530937">statusBar</a>()->message( msg ); printer->newPage(); // no more room on this page yPos = 0; // back to top of page } p.<a href="qpainter.html#0f088f">drawText</a>( Margin, Margin + yPos, metrics.<a href="qpaintdevicemetrics.html#95eb7b">width</a>(), fm.<a href="qfontmetrics.html#e6e380">lineSpacing</a>(), ExpandTabs | DontClip, e->textLine( i ) ); yPos = yPos + fm.<a href="qfontmetrics.html#e6e380">lineSpacing</a>(); } p.<a href="qpainter.html#365784">end</a>(); // send job to printer <a href="qmainwindow.html#530937">statusBar</a>()->message( "Printing completed", 2000 ); } else { <a href="qmainwindow.html#530937">statusBar</a>()->message( "Printing aborted", 2000 ); }}void <a name="437"></a>ApplicationWindow::closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* ce ){ if ( !e->edited() ) { ce-><a href="qcloseevent.html#7cf66f">accept</a>(); return; } switch( <a href="qmessagebox.html#66b7c8">QMessageBox::information</a>( this, "Qt Application Example", "The document has been changed since " "the last save.", "Save Now", "Cancel", "Leave Anyway", 0, 1 ) ) { case 0: <a href=#434>save</a>(); ce-><a href="qcloseevent.html#7cf66f">accept</a>(); break; case 1: default: // just for sanity ce-><a href="qcloseevent.html#ee1736">ignore</a>(); break; case 2: ce-><a href="qcloseevent.html#7cf66f">accept</a>(); break; }}void <a name="438"></a>ApplicationWindow::about(){ <a href="qmessagebox.html#f6c3cd">QMessageBox::about</a>( this, "Qt Application Example", "This example demonstrates simple use of " "QMainWindow,\nQMenuBar and QToolBar.");}void <a name="439"></a>ApplicationWindow::aboutQt(){ <a href="qmessagebox.html#b72270">QMessageBox::aboutQt</a>( this, "Qt Application Example" );}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/application/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include "application.h"int main( int argc, char ** argv ) { <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv ); ApplicationWindow * mw = new ApplicationWindow(); mw-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - Application" ); mw-><a name="show"></a><a href="qmainwindow.html#eb53e3">show</a>(); a.<a name="connect"></a><a href="qobject.html#fbde73">connect</a>( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) ); return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -