mdi-main-cpp.html
来自「qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人」· HTML 代码 · 共 506 行 · 第 1/2 页
HTML
506 行
{ MDIWindow* w = new MDIWindow( ws, 0, WDestructiveClose ); <a href="qobject.html#fbde73">connect</a>( w, SIGNAL( message(const QString&, int) ), <a href="qmainwindow.html#530937">statusBar</a>(), SLOT( message(const QString&, int )) ); w-><a href="qwidget.html#d6a291">setCaption</a>("unnamed document"); w-><a href="qwidget.html#504fc4">setIcon</a>( <a href="qpixmap.html">QPixmap</a>("document.xpm") ); // show the very first window in maximized mode if ( ws->windowList().isEmpty() ) w-><a href="qwidget.html#b6e000">showMaximized</a>(); else w-><a href="qwidget.html#200ee5">show</a>(); return w;}void <a name="163"></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>() ) { MDIWindow* w = newDoc(); w->load( fn ); } else { <a href="qmainwindow.html#530937">statusBar</a>()->message( "Loading aborted", 2000 ); }}void <a name="164"></a>ApplicationWindow::save(){ MDIWindow* m = (MDIWindow*)ws->activeWindow(); if ( m ) m->save();}void <a name="165"></a>ApplicationWindow::saveAs(){ MDIWindow* m = (MDIWindow*)ws->activeWindow(); if ( m ) m->saveAs();}void <a name="166"></a>ApplicationWindow::print(){#ifndef QT_NO_PRINTER MDIWindow* m = (MDIWindow*)ws->activeWindow(); if ( m ) m->print( printer );#endif}void <a name="167"></a>ApplicationWindow::closeWindow(){ MDIWindow* m = (MDIWindow*)ws->activeWindow(); if ( m ) m-><a href="qwidget.html#3d9c87">close</a>();}void <a name="168"></a>ApplicationWindow::about(){ <a href="qmessagebox.html#f6c3cd">QMessageBox::about</a>( this, "Qt Application Example", "This example demonstrates simple use of\n " "Qt's Multiple Document Interface (MDI).");}void <a name="169"></a>ApplicationWindow::aboutQt(){ <a href="qmessagebox.html#b72270">QMessageBox::aboutQt</a>( this, "Qt Application Example" );}void <a name="170"></a>ApplicationWindow::windowsMenuAboutToShow(){ windowsMenu->clear(); int cascadeId = windowsMenu->insertItem("&Cascade", ws, SLOT(cascade() ) ); int tileId = windowsMenu->insertItem("&Tile", ws, SLOT(tile() ) ); if ( ws->windowList().isEmpty() ) { windowsMenu->setItemEnabled( cascadeId, FALSE ); windowsMenu->setItemEnabled( tileId, FALSE ); } windowsMenu->insertSeparator(); QWidgetList windows = ws->windowList(); for ( int i = 0; i < int(windows.count()); ++i ) { int id = windowsMenu->insertItem(windows.at(i)->caption(), this, SLOT( <a href=#171>windowsMenuActivated</a>( int ) ) ); windowsMenu->setItemParameter( id, i ); windowsMenu->setItemChecked( id, ws->activeWindow() == windows.at(i) ); }}void <a name="171"></a>ApplicationWindow::windowsMenuActivated( int id ){ <a href="qwidget.html">QWidget</a>* w = ws->windowList().at( id ); if ( w ) { w-><a href="qwidget.html#8a8f06">showNormal</a>(); w-><a href="qwidget.html#25775a">setFocus</a>(); }}MDIWindow::MDIWindow( <a href="qwidget.html">QWidget</a>* parent, const char* name, int wflags ) : <a href="qmainwindow.html">QMainWindow</a>( parent, name, wflags ){ mmovie = 0; medit = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this ); <a href="qwidget.html#cddadb">setFocusProxy</a>( medit ); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>( medit );}MDIWindow::~MDIWindow(){ delete mmovie;}void <a name="158"></a>MDIWindow::load( const QString& fn ){ filename = fn; <a href="qfile.html">QFile</a> f( filename ); if ( !f.<a href="qfile.html#255995">open</a>( IO_ReadOnly ) ) return; if(fn.<a href="qstring.html#3c8233">contains</a>(".gif")) { <a href="qwidget.html">QWidget</a> * tmp=new <a href="qwidget.html">QWidget</a>(this); <a href="qwidget.html#cddadb">setFocusProxy</a>(tmp); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>(tmp); medit->hide(); delete medit; <a href="qmovie.html">QMovie</a> * qm=new <a href="qmovie.html">QMovie</a>(fn);#ifdef _WS_QWS_ // temporary speed-test hack qm->setDisplayWidget(tmp);#endif tmp-><a href="qwidget.html#e84bbe">setBackgroundMode</a>(QWidget::NoBackground); tmp-><a href="qwidget.html#200ee5">show</a>(); mmovie=qm; } else { mmovie = 0; medit->setAutoUpdate( FALSE ); medit->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>(); medit->append( s ); } f.<a href="qfile.html#64e640">close</a>(); medit->setAutoUpdate( TRUE ); medit->repaint(); } <a href="qwidget.html#d6a291">setCaption</a>( filename ); emit message( <a href="qstring.html">QString</a>("Loaded document %1").arg(filename), 2000 );}void <a name="159"></a>MDIWindow::save(){ if ( filename.isEmpty() ) { <a href=#160>saveAs</a>(); return; } <a href="qstring.html">QString</a> text = medit->text(); <a href="qfile.html">QFile</a> f( filename ); if ( !f.<a href="qfile.html#255995">open</a>( IO_WriteOnly ) ) { emit 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>(); <a href="qwidget.html#d6a291">setCaption</a>( filename ); emit message( <a href="qstring.html">QString</a>( "File %1 saved" ).arg( filename ), 2000 );}void <a name="160"></a>MDIWindow::saveAs(){ <a href="qstring.html">QString</a> fn = QFileDialog::getSaveFileName( filename, QString::null, this ); if ( !fn.<a href="qstring.html#c62623">isEmpty</a>() ) { filename = fn; <a href=#159>save</a>(); } else { emit message( "Saving aborted", 2000 ); }}void <a name="161"></a>MDIWindow::print( <a href="qprinter.html">QPrinter</a>* printer){#ifndef QT_NO_PRINTER const int Margin = 10; int pageNo = 1; if ( printer-><a href="qprinter.html#c3cd23">setup</a>(this) ) { // printer dialog emit message( "Printing...", 0 ); <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>( medit->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 < medit->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 += ")..."; emit message( msg, 0 ); printer-><a href="qprinter.html#d4f693">newPage</a>(); // 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, medit->textLine( i ) ); yPos = yPos + fm.<a href="qfontmetrics.html#e6e380">lineSpacing</a>(); } p.<a href="qpainter.html#365784">end</a>(); // send job to printer emit message( "Printing completed", 2000 ); } else { emit message( "Printing aborted", 2000 ); }#endif}</pre> <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/mdi/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 - Multiple Documents Interface (MDI)" ); 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()) ); int res = a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>(); return res;}</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?