simple-application-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 422 行 · 第 1/2 页

HTML
422
字号
    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Help", help );    help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;About", this, SLOT(about()), Key_F1 );    help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About &amp;Qt", this, SLOT(aboutQt()) );    help-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();    help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "What's &amp;This", this, SLOT(<a href="qmainwindow.html#whatsThis">whatsThis</a>()), SHIFT+Key_F1 );    e = new <a href="qtextedit.html">QTextEdit</a>( this, "editor" );    e-&gt;<a href="qwidget.html#setFocus">setFocus</a>();    <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( e );    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Ready", 2000 );    <a href="qwidget.html#resize">resize</a>( 450, 600 );}ApplicationWindow::~ApplicationWindow(){    delete printer;}void <a name="f449"></a>ApplicationWindow::newDoc(){    ApplicationWindow *ed = new ApplicationWindow;    ed-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Application");    ed-&gt;<a href="qwidget.html#show">show</a>();}void <a name="f450"></a>ApplicationWindow::choose(){<a name="x1512"></a>    <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null,                                               this);    if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )        load( fn );    else        <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loading aborted", 2000 );}void <a name="f451"></a>ApplicationWindow::load( const <a href="qstring.html">QString</a> &amp;fileName ){    <a href="qfile.html">QFile</a> f( fileName );<a name="x1511"></a>    if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) )        return;    <a href="qtextstream.html">QTextStream</a> ts( &amp;f );<a name="x1540"></a>    e-&gt;<a href="qtextedit.html#setText">setText</a>( ts.<a href="qtextstream.html#read">read</a>() );<a name="x1537"></a>    e-&gt;<a href="qtextedit.html#setModified">setModified</a>( FALSE );    <a href="qwidget.html#setCaption">setCaption</a>( fileName );    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Loaded document " + fileName, 2000 );}void <a name="f452"></a>ApplicationWindow::save(){    if ( filename.isEmpty() ) {        saveAs();        return;    }<a name="x1539"></a>    <a href="qstring.html">QString</a> text = e-&gt;<a href="qtextedit.html#text">text</a>();    <a href="qfile.html">QFile</a> f( filename );    if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {        <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( QString("Could not write to %1").arg(filename),                              2000 );        return;    }    <a href="qtextstream.html">QTextStream</a> t( &amp;f );    t &lt;&lt; text;<a name="x1510"></a>    f.<a href="qfile.html#close">close</a>();    e-&gt;<a href="qtextedit.html#setModified">setModified</a>( FALSE );    <a href="qwidget.html#setCaption">setCaption</a>( filename );    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( QString( "File %1 saved" ).arg( filename ), 2000 );}void <a name="f453"></a>ApplicationWindow::saveAs(){<a name="x1513"></a>    <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>()-&gt;message( "Saving aborted", 2000 );    }}void <a name="f454"></a>ApplicationWindow::print(){    // ###### Rewrite to use QSimpleRichText to print here as well    const int Margin = 10;    int pageNo = 1;<a name="x1531"></a>    if ( printer-&gt;<a href="qprinter.html#setup">setup</a>(this) ) {               // printer dialog        <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing..." );        <a href="qpainter.html">QPainter</a> p;<a name="x1525"></a>        if( !p.<a href="qpainter.html#begin">begin</a>( printer ) )               // paint on printer            return;<a name="x1534"></a><a name="x1529"></a>        p.<a href="qpainter.html#setFont">setFont</a>( e-&gt;<a href="qtextedit.html#font">font</a>() );        int yPos        = 0;                    // y-position for each line<a name="x1528"></a>        <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<a name="x1536"></a>        for( int i = 0 ; i &lt; e-&gt;<a href="qtextedit.html#lines">lines</a>() ; i++ ) {<a name="x1523"></a>            if ( Margin + yPos &gt; metrics.<a href="qpaintdevicemetrics.html#height">height</a>() - Margin ) {                <a href="qstring.html">QString</a> msg( "Printing (page " );<a name="x1533"></a>                msg += QString::<a href="qstring.html#number">number</a>( ++pageNo );                msg += ")...";                <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( msg );<a name="x1530"></a>                printer-&gt;<a href="qprinter.html#newPage">newPage</a>();             // no more room on this page                yPos = 0;                       // back to top of page            }            p.<a href="qpainter.html#drawText">drawText</a>( Margin, Margin + yPos,<a name="x1524"></a><a name="x1514"></a>                        metrics.<a href="qpaintdevicemetrics.html#width">width</a>(), fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>(),                        ExpandTabs | DontClip,                        e-&gt;<a href="qtextedit.html#text">text</a>( i ) );            yPos = yPos + fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>();        }<a name="x1527"></a>        p.<a href="qpainter.html#end">end</a>();                                // send job to printer        <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing completed", 2000 );    } else {        <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Printing aborted", 2000 );    }}<a name="x1543"></a>void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a>* ce ){<a name="x1535"></a>    if ( !e-&gt;<a href="qtextedit.html#isModified">isModified</a>() ) {<a name="x1508"></a>        ce-&gt;<a href="qcloseevent.html#accept">accept</a>();        return;    }<a name="x1521"></a>    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 ) ) {    case 0:        save();        ce-&gt;<a href="qcloseevent.html#accept">accept</a>();        break;    case 1:        ce-&gt;<a href="qcloseevent.html#accept">accept</a>();        break;    case 2:    default: // just for sanity<a name="x1509"></a>        ce-&gt;<a href="qcloseevent.html#ignore">ignore</a>();        break;    }}void <a name="f455"></a>ApplicationWindow::about(){<a name="x1519"></a>    QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Qt Application Example",                        "This example demonstrates simple use of "                        "QMainWindow,\nQMenuBar and QToolBar.");}void <a name="f456"></a>ApplicationWindow::aboutQt(){<a name="x1520"></a>    QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Application Example" );}</pre><p> <hr><p> Main:<p> <pre>/****************************************************************************** $Id:  qt/main.cpp   3.0.5   edited Oct 12 2001 $**** 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 href="qapplication-h.html">qapplication.h</a>&gt;#include "application.h"int main( int argc, char ** argv ) {    <a href="qapplication.html">QApplication</a> a( argc, argv );    ApplicationWindow * mw = new ApplicationWindow();    mw-&gt;<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Application" );    mw-&gt;<a href="qwidget.html#show">show</a>();<a name="x1550"></a>    a.<a href="qobject.html#connect">connect</a>( &amp;a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &amp;a, SLOT(<a href="qapplication.html#quit">quit</a>()) );    return a.<a href="qapplication.html#exec">exec</a>();}</pre><p>See also <a href="examples.html">Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 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 + -
显示快捷键?