<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/application/application.doc:5 --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>A Complete Application Window</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 Classes</font></a> | <a href="mainclasses.html"> <font color="#004faf">Main Classes</font></a> | <a href="annotated.html"> <font color="#004faf">Annotated</font></a> | <a href="groups.html"> <font color="#004faf">Grouped 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</h1> <p> <p> This example program looks like a complete modern application. It has a menu bar, it has a tool bar, it has a status bar and works like a simple text editor. <p> There is a <a href="simple-application.html">walkthrough</a> of this example. <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 <<a href="qmainwindow-h.html">qmainwindow.h</a>> 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> &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 <<a href="qimage-h.html">qimage.h</a>> #include <<a href="qpixmap-h.html">qpixmap.h</a>> #include <<a href="qtoolbar-h.html">qtoolbar.h</a>> #include <<a href="qtoolbutton-h.html">qtoolbutton.h</a>> #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>> #include <<a href="qmenubar-h.html">qmenubar.h</a>> #include <<a href="qtextedit-h.html">qtextedit.h</a>> #include <<a href="qfile-h.html">qfile.h</a>> #include <<a href="qfiledialog-h.html">qfiledialog.h</a>> #include <<a href="qstatusbar-h.html">qstatusbar.h</a>> #include <<a href="qmessagebox-h.html">qmessagebox.h</a>> #include <<a href="qprinter-h.html">qprinter.h</a>> #include <<a href="qapplication-h.html">qapplication.h</a>> #include <<a href="qaccel-h.html">qaccel.h</a>> #include <<a href="qtextstream-h.html">qtextstream.h</a>> #include <<a href="qpainter-h.html">qpainter.h</a>> #include <<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>> #include <<a href="qwhatsthis-h.html">qwhatsthis.h</a>> #include "filesave.xpm" #include "fileopen.xpm" #include "fileprint.xpm" <a name="f501"></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="qpixmap.html">QPixmap</a> openIcon, saveIcon, printIcon; <a href="qtoolbar.html">QToolBar</a> * fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" ); <a name="x1945"></a> fileTools-><a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" ); openIcon = QPixmap( fileopen ); QToolButton * fileOpen = new <a href="qtoolbutton.html">QToolButton</a>( openIcon, "Open File", <a href="qstring.html#QString-null">QString::null</a>, this, SLOT(choose()), fileTools, "open file" ); saveIcon = QPixmap( filesave ); QToolButton * fileSave = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null, this, SLOT(save()), fileTools, "save file" ); printIcon = QPixmap( fileprint ); QToolButton * filePrint = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null, this, SLOT(print()), fileTools, "print file" ); (void)QWhatsThis::whatsThisButton( fileTools ); const char * fileOpenText = "<p><img source=\"fileopen\"> " "Click this button to open a <em>new file</em>. <br>" "You can also select the <b>Open</b> command " "from the <b>File</b> menu.</p>"; <a name="x1946"></a> QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileOpen, fileOpenText ); <a name="x1926"></a> QMimeSourceFactory::<a href="qmimesourcefactory.html#defaultFactory">defaultFactory</a>()->setPixmap( "fileopen", openIcon ); const char * fileSaveText = "<p>Click this button to save the file you " "are editing. You will be prompted for a file name.\n" "You can also select the <b>Save</b> command " "from the <b>File</b> menu.</p>"; QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileSave, fileSaveText ); 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."; QWhatsThis::<a href="qwhatsthis.html#add">add</a>( filePrint, filePrintText ); <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( "&File", file ); file-><a href="qmenudata.html#insertItem">insertItem</a>( "&New", this, SLOT(newDoc()), CTRL+Key_N ); int id; id = file-><a href="qmenudata.html#insertItem">insertItem</a>( openIcon, "&Open...", this, SLOT(choose()), CTRL+Key_O ); <a name="x1922"></a> file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileOpenText ); id = file-><a href="qmenudata.html#insertItem">insertItem</a>( saveIcon, "&Save", this, SLOT(save()), CTRL+Key_S ); file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText ); id = file-><a href="qmenudata.html#insertItem">insertItem</a>( "Save &As...", this, SLOT(saveAs()) ); file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, fileSaveText ); file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>(); id = file-><a href="qmenudata.html#insertItem">insertItem</a>( printIcon, "&Print...", this, SLOT(print()), CTRL+Key_P ); file-><a href="qmenudata.html#setWhatsThis">setWhatsThis</a>( id, filePrintText ); file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>(); file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Close", this, SLOT(<a href="qwidget.html#close">close</a>()), CTRL+Key_W ); <a name="x1912"></a> file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Quit", qApp, SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q ); <a href="qmainwindow.html#menuBar">menuBar</a>()->insertSeparator(); <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( "&Help", help ); help-><a href="qmenudata.html#insertItem">insertItem</a>( "&About", this, SLOT(about()), Key_F1 ); help-><a href="qmenudata.html#insertItem">insertItem</a>( "About &Qt", this, SLOT(aboutQt()) ); help-><a href="qmenudata.html#insertSeparator">insertSeparator</a>(); help-><a href="qmenudata.html#insertItem">insertItem</a>( "What's &This", this, SLOT(<a href="qmainwindow.html#whatsThis">whatsThis</a>()), SHIFT+Key_F1 ); e = new <a href="qtextedit.html">QTextEdit</a>( this, "editor" ); e-><a href="qwidget.html#setFocus">setFocus</a>(); <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( e ); <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Ready", 2000 ); <a href="qwidget.html#resize">resize</a>( 450, 600 ); } ApplicationWindow::~ApplicationWindow() { delete printer; } void <a name="f502"></a>ApplicationWindow::newDoc() { ApplicationWindow *ed = new ApplicationWindow; ed-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Application"); ed-><a href="qwidget.html#show">show</a>(); } void <a name="f503"></a>ApplicationWindow::choose() { <a name="x1917"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null, this); <a name="x1936"></a> if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) load( fn ); else <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Loading aborted", 2000 ); } void <a name="f504"></a>ApplicationWindow::load( const <a href="qstring.html">QString</a> &fileName ) { <a href="qfile.html">QFile</a> f( fileName ); <a name="x1916"></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( &f ); <a name="x1944"></a> e-><a href="qtextedit.html#setText">setText</a>( ts.<a href="qtextstream.html#read">read</a>() ); <a name="x1941"></a> e-><a href="qtextedit.html#setModified">setModified</a>( FALSE ); <a href="qwidget.html#setCaption">setCaption</a>( fileName ); <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Loaded document " + fileName, 2000 ); } void <a name="f505"></a>ApplicationWindow::save() { if ( filename.isEmpty() ) { saveAs(); return; } <a name="x1943"></a> <a href="qstring.html">QString</a> text = e-><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>()->message( QString("Could not write to %1").arg(filename), 2000 ); return; } <a href="qtextstream.html">QTextStream</a> t( &f ); t << text; <a name="x1915"></a> f.<a href="qfile.html#close">close</a>(); e-><a href="qtextedit.html#setModified">setModified</a>( FALSE ); <a href="qwidget.html#setCaption">setCaption</a>( filename ); <a href="qmainwindow.html#statusBar">statusBar</a>()->message( QString( "File %1 saved" ).arg( filename ), 2000 ); } void <a name="f506"></a>ApplicationWindow::saveAs() { <a name="x1918"></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>()->message( "Saving aborted", 2000 ); } } void <a name="f507"></a>ApplicationWindow::print() { // ###### Rewrite to use QSimpleRichText to print here as well const int Margin = 10; int pageNo = 1; <a name="x1935"></a> if ( printer-><a href="qprinter.html#setup">setup</a>(this) ) { // printer dialog <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing..." ); <a href="qpainter.html">QPainter</a> p; <a name="x1929"></a> if( !p.<a href="qpainter.html#begin">begin</a>( printer ) ) // paint on printer return; <a name="x1938"></a><a name="x1933"></a> p.<a href="qpainter.html#setFont">setFont</a>( e-><a href="qtextedit.html#font">font</a>() ); int yPos = 0; // y-position for each line <a name="x1932"></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="x1940"></a> for( int i = 0 ; i < e-><a href="qtextedit.html#lines">lines</a>() ; i++ ) { <a name="x1927"></a> if ( Margin + yPos > metrics.<a href="qpaintdevicemetrics.html#height">height</a>() - Margin ) { <a href="qstring.html">QString</a> msg( "Printing (page " ); <a name="x1937"></a> msg += QString::<a href="qstring.html#number">number</a>( ++pageNo ); msg += ")..."; <a href="qmainwindow.html#statusBar">statusBar</a>()->message( msg ); <a name="x1934"></a> printer-><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="x1928"></a><a name="x1919"></a> metrics.<a href="qpaintdevicemetrics.html#width">width</a>(), fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>(), ExpandTabs | DontClip, e-><a href="qtextedit.html#text">text</a>( i ) ); yPos = yPos + fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>(); } <a name="x1931"></a> p.<a href="qpainter.html#end">end</a>(); // send job to printer <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing completed", 2000 ); } else { <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Printing aborted", 2000 ); } } <a name="x1947"></a>void ApplicationWindow::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a>* ce ) { <a name="x1939"></a> if ( !e-><a href="qtextedit.html#isModified">isModified</a>() ) { <a name="x1913"></a> ce-><a href="qcloseevent.html#accept">accept</a>(); return; } <a name="x1925"></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-><a href="qcloseevent.html#accept">accept</a>(); break; case 1: ce-><a href="qcloseevent.html#accept">accept</a>(); break; case 2: default: // just for sanity <a name="x1914"></a> ce-><a href="qcloseevent.html#ignore">ignore</a>(); break; } } void <a name="f508"></a>ApplicationWindow::about() { <a name="x1923"></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="f509"></a>ApplicationWindow::aboutQt() { <a name="x1924"></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 <<a href="qapplication-h.html">qapplication.h</a>> #include "application.h" int main( int argc, char ** argv ) { <a href="qapplication.html">QApplication</a> a( argc, argv ); ApplicationWindow * mw = new ApplicationWindow(); mw-><a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Application" ); mw-><a href="qwidget.html#show">show</a>(); <a name="x1954"></a><a name="x1952"></a> a.<a href="qobject.html#connect">connect</a>( &a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &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 © 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>