Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 849

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/action/application.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A Complete Application Window with Actions</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A Complete Application Window with Actions</h1>

 
<p> 
<p> This example program is just like the application example,
but uses <a href="qaction.html">QAction</a> to build the menu and the toolbar.
<p> The QAction related part of the program is covered in
detail by the <a href="simple-application-action.html">
QAction application walkthrough</a> whilst the 
<a href="simple-application.html">Simple application walkthrough</a>
deals with the rest.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id:  qt/application.h   3.0.2   edited Oct 12 12:18 $
**
** 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.
**
*****************************************************************************/

#ifndef APPLICATION_H
#define APPLICATION_H

#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;

class QTextEdit;

class ApplicationWindow: public <a href="qmainwindow.html">QMainWindow</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

public:
    ApplicationWindow();
    ~ApplicationWindow();

protected:
    void closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* );

private slots:
    void newDoc();
    void choose();
    void load( const <a href="qstring.html">QString</a> &amp;fileName );
    void save();
    void saveAs();
    void print();

    void about();
    void aboutQt();

private:
    <a href="qprinter.html">QPrinter</a> *printer;
    <a href="qtextedit.html">QTextEdit</a> *e;
    <a href="qstring.html">QString</a> filename;
};


#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id:  qt/application.cpp   3.0.2   edited Oct 12 12:18 $
**
** 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 "application.h"

#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qtextedit-h.html">qtextedit.h</a>&gt;
#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qaccel-h.html">qaccel.h</a>&gt;
#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>&gt;
#include &lt;<a href="qwhatsthis-h.html">qwhatsthis.h</a>&gt;
#include &lt;<a href="qaction-h.html">qaction.h</a>&gt;

#include "filesave.xpm"
#include "fileopen.xpm"
#include "fileprint.xpm"


<a name="f537"></a>ApplicationWindow::ApplicationWindow()
    : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose )
{
    printer = new <a href="qprinter.html">QPrinter</a>;

    <a href="qaction.html">QAction</a> * fileNewAction;
    <a href="qaction.html">QAction</a> * fileOpenAction;
    <a href="qaction.html">QAction</a> * fileSaveAction, * fileSaveAsAction, * filePrintAction;
    <a href="qaction.html">QAction</a> * fileCloseAction, * fileQuitAction;

    fileNewAction = new <a href="qaction.html">QAction</a>( "New", "&amp;New", CTRL+Key_N, this, "new" );
<a name="x2011"></a>    <a href="qobject.html#connect">connect</a>( fileNewAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , this,
             SLOT( newDoc() ) );

    fileOpenAction = new <a href="qaction.html">QAction</a>( "Open File", QPixmap( fileopen ), "&amp;Open",
                                  CTRL+Key_O, this, "open" );
    <a href="qobject.html#connect">connect</a>( fileOpenAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , this, SLOT( choose() ) );

    const char * fileOpenText = "&lt;p&gt;&lt;img source=\"fileopen\"&gt; "
                     "Click this button to open a &lt;em&gt;new file&lt;/em&gt;. &lt;br&gt;"
                     "You can also select the &lt;b&gt;Open&lt;/b&gt; command "
                     "from the &lt;b&gt;File&lt;/b&gt; menu.&lt;/p&gt;";
<a name="x2028"></a>    QMimeSourceFactory::<a href="qmimesourcefactory.html#defaultFactory">defaultFactory</a>()-&gt;setPixmap( "fileopen",
<a name="x2013"></a>                          fileOpenAction-&gt;<a href="qaction.html#iconSet">iconSet</a>().pixmap() );
<a name="x2014"></a>    fileOpenAction-&gt;<a href="qaction.html#setWhatsThis">setWhatsThis</a>( fileOpenText );

    fileSaveAction = new <a href="qaction.html">QAction</a>( "Save File", QPixmap( filesave ),
                                  "&amp;Save", CTRL+Key_S, this, "save" );
    <a href="qobject.html#connect">connect</a>( fileSaveAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , this, SLOT( save() ) );

    const char * fileSaveText = "&lt;p&gt;Click this button to save the file you "
                     "are editing. You will be prompted for a file name.\n"
                     "You can also select the &lt;b&gt;Save&lt;/b&gt; command "
                     "from the &lt;b&gt;File&lt;/b&gt; menu.&lt;/p&gt;";
    fileSaveAction-&gt;<a href="qaction.html#setWhatsThis">setWhatsThis</a>( fileSaveText );

    fileSaveAsAction = new <a href="qaction.html">QAction</a>( "Save File As", "Save &amp;as", 0,  this,
                                    "save as" );
    <a href="qobject.html#connect">connect</a>( fileSaveAsAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , this,
             SLOT( saveAs() ) );
    fileSaveAsAction-&gt;<a href="qaction.html#setWhatsThis">setWhatsThis</a>( fileSaveText );

    filePrintAction = new <a href="qaction.html">QAction</a>( "Print File", QPixmap( fileprint ),
                                   "&amp;Print", CTRL+Key_P, this, "print" );
    <a href="qobject.html#connect">connect</a>( filePrintAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , this,
             SLOT( print() ) );

    const char * filePrintText = "Click this button to print the file you "
                     "are editing.\n You can also select the Print "
                     "command from the File menu.";
    filePrintAction-&gt;<a href="qaction.html#setWhatsThis">setWhatsThis</a>( filePrintText );

    fileCloseAction = new <a href="qaction.html">QAction</a>( "Close", "&amp;Close", CTRL+Key_W, this,
                                   "close" );
    <a href="qobject.html#connect">connect</a>( fileCloseAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , this,
             SLOT( <a href="qwidget.html#close">close</a>() ) );

    fileQuitAction = new <a href="qaction.html">QAction</a>( "Quit", "&amp;Quit", CTRL+Key_Q, this,
                                  "quit" );
<a name="x2015"></a>    <a href="qobject.html#connect">connect</a>( fileQuitAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ) , qApp,
             SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ) );

    // populate a tool bar with some actions

    <a href="qtoolbar.html">QToolBar</a> * fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
<a name="x2047"></a>    fileTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" );
<a name="x2012"></a>    fileOpenAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
    fileSaveAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
    filePrintAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
    (void)QWhatsThis::whatsThisButton( fileTools );


    // populate a menu with all actions

    <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;File", file );
    fileNewAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );
    fileOpenAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );
    fileSaveAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );
    fileSaveAsAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );
    file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    filePrintAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );
    file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    fileCloseAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );
    fileQuitAction-&gt;<a href="qaction.html#addTo">addTo</a>( file );


    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertSeparator();

    // add a help menu

    <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <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 );


    // create and define the central widget

    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="f538"></a>ApplicationWindow::newDoc()
{
    ApplicationWindow *ed = new ApplicationWindow;
    ed-&gt;<a href="qwidget.html#show">show</a>();
}

void <a name="f539"></a>ApplicationWindow::choose()
{
<a name="x2020"></a>    <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( <a href="qstring.html#QString-null">QString::null</a>, 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="f540"></a>ApplicationWindow::load( const <a href="qstring.html">QString</a> &amp;fileName )
{
    <a href="qfile.html">QFile</a> f( fileName );
<a name="x2019"></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="x2046"></a>    e-&gt;<a href="qtextedit.html#setText">setText</a>( ts.<a href="qtextstream.html#read">read</a>() );
<a name="x2043"></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="f541"></a>ApplicationWindow::save()
{
    if ( filename.isEmpty() ) {
        saveAs();
        return;
    }

<a name="x2045"></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;
    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="f542"></a>ApplicationWindow::saveAs()
{
<a name="x2021"></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="f543"></a>ApplicationWindow::print()
{
    const int Margin = 10;
    int pageNo = 1;

<a name="x2037"></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;
        if( !p.<a href="qpainter.html#begin">begin</a>( printer ) )              // paint on printer
            return;

<a name="x2040"></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="x2034"></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="x2042"></a>        for( int i = 0 ; i &lt; e-&gt;<a href="qtextedit.html#lines">lines</a>() ; i++ ) {
<a name="x2029"></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="x2039"></a>                msg += QString::<a href="qstring.html#number">number</a>( ++pageNo );
                msg += ")...";
                <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( msg );
<a name="x2036"></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="x2030"></a><a name="x2022"></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>();
        }
        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="x2048"></a>void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a>* ce )
{
<a name="x2041"></a>    if ( !e-&gt;<a href="qtextedit.html#isModified">isModified</a>() ) {
<a name="x2016"></a>        ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
        return;
    }

<a name="x2027"></a>    switch( QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Qt Application Example",
                                      "The document has been changed since "
                                      "the last save.",
                                      "Save Now", "Cancel", "Leave Anyway",
                                      0, 1 ) ) {
    case 0:
        save();
        ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
        break;
    case 1:
    default: // just for sanity
<a name="x2017"></a>        ce-&gt;<a href="qcloseevent.html#ignore">ignore</a>();
        break;
    case 2:
        ce-&gt;<a href="qcloseevent.html#accept">accept</a>();
        break;
    }
}


void <a name="f544"></a>ApplicationWindow::about()
{
<a name="x2025"></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="f545"></a>ApplicationWindow::aboutQt()
{
<a name="x2026"></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.2   edited Oct 12 12:18 $
**
** 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>( "Document 1" );
    mw-&gt;<a href="qwidget.html#show">show</a>();
<a name="x2054"></a><a name="x2052"></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="qaction-examples.html">QAction Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<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.2</div>
</table></div></address></body>
</html>