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&amp;, int) ), <a href="qmainwindow.html#530937">statusBar</a>(), SLOT( message(const QString&amp;, int )) );    w-&gt;<a href="qwidget.html#d6a291">setCaption</a>("unnamed document");    w-&gt;<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-&gt;windowList().isEmpty() )        w-&gt;<a href="qwidget.html#b6e000">showMaximized</a>();    else        w-&gt;<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-&gt;load( fn );    }  else {        <a href="qmainwindow.html#530937">statusBar</a>()-&gt;message( "Loading aborted", 2000 );    }}void <a name="164"></a>ApplicationWindow::save(){    MDIWindow* m = (MDIWindow*)ws-&gt;activeWindow();    if ( m )        m-&gt;save();}void <a name="165"></a>ApplicationWindow::saveAs(){    MDIWindow* m = (MDIWindow*)ws-&gt;activeWindow();    if ( m )        m-&gt;saveAs();}void <a name="166"></a>ApplicationWindow::print(){#ifndef QT_NO_PRINTER    MDIWindow* m = (MDIWindow*)ws-&gt;activeWindow();    if ( m )        m-&gt;print( printer );#endif}void <a name="167"></a>ApplicationWindow::closeWindow(){    MDIWindow* m = (MDIWindow*)ws-&gt;activeWindow();    if ( m )        m-&gt;<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-&gt;clear();    int cascadeId = windowsMenu-&gt;insertItem("&amp;Cascade", ws, SLOT(cascade() ) );    int tileId = windowsMenu-&gt;insertItem("&amp;Tile", ws, SLOT(tile() ) );    if ( ws-&gt;windowList().isEmpty() ) {        windowsMenu-&gt;setItemEnabled( cascadeId, FALSE );        windowsMenu-&gt;setItemEnabled( tileId, FALSE );    }    windowsMenu-&gt;insertSeparator();    QWidgetList windows = ws-&gt;windowList();    for ( int i = 0; i &lt; int(windows.count()); ++i ) {        int id = windowsMenu-&gt;insertItem(windows.at(i)-&gt;caption(),                                         this, SLOT( <a href=#171>windowsMenuActivated</a>( int ) ) );        windowsMenu-&gt;setItemParameter( id, i );        windowsMenu-&gt;setItemChecked( id, ws-&gt;activeWindow() == windows.at(i) );    }}void <a name="171"></a>ApplicationWindow::windowsMenuActivated( int id ){    <a href="qwidget.html">QWidget</a>* w = ws-&gt;windowList().at( id );    if ( w ) {        w-&gt;<a href="qwidget.html#8a8f06">showNormal</a>();        w-&gt;<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&amp; 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-&gt;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-&gt;setDisplayWidget(tmp);#endif        tmp-&gt;<a href="qwidget.html#e84bbe">setBackgroundMode</a>(QWidget::NoBackground);        tmp-&gt;<a href="qwidget.html#200ee5">show</a>();        mmovie=qm;    } else {        mmovie = 0;        medit-&gt;setAutoUpdate( FALSE );        medit-&gt;clear();        <a href="qtextstream.html">QTextStream</a> t(&amp;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-&gt;append( s );        }        f.<a href="qfile.html#64e640">close</a>();        medit-&gt;setAutoUpdate( TRUE );        medit-&gt;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-&gt;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( &amp;f );    t &lt;&lt; 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-&gt;<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-&gt;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 &lt; medit-&gt;numLines() ; i++ ) {            if ( Margin + yPos &gt; 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-&gt;<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-&gt;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>/****************************************************************************** &#36;Id&#58; 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 &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;#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-&gt;<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - Multiple Documents Interface (MDI)" );    mw-&gt;<a name="show"></a><a href="qmainwindow.html#eb53e3">show</a>();    a.<a name="connect"></a><a href="qobject.html#fbde73">connect</a>( &amp;a, SIGNAL(lastWindowClosed()), &amp;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 + -
显示快捷键?