simple-application.html

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

HTML
618
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/doc/application-walkthrough.doc:36 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Walkthrough: A Simple Application</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>Walkthrough: A Simple Application</h1> <p> <p> This walkthrough shows simple use of <a href="qmainwindow.html">QMainWindow</a>, <a href="qmenubar.html">QMenuBar</a>, <a href="qpopupmenu.html">QPopupMenu</a>, <a href="qtoolbar.html">QToolBar</a> and <a href="qstatusbar.html">QStatusBar</a> - classes that everymodern application window tends to use.<p> It further illustrates some aspects of <a href="qwhatsthis.html">QWhatsThis</a> (for simple help) and atypical <em>main()</em> using <a href="qapplication.html">QApplication</a>.<p> Finally, it shows a typical printout function based on <a href="qprinter.html">QPrinter</a>.<p> <h2>The declaration of ApplicationWindow</h2><p> Here's the header file in full:<p> <pre>/****************************************************************************** $Id:  qt/application.h   3.0.5   edited May 7 17:30 $**** 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> It declares a class that inherits <a href="qmainwindow.html">QMainWindow</a>, with slots and privatevariables.  The class predeclaration of <a href="qtextedit.html">QTextEdit</a> at the beginning(instead of an include) helps to speed up compiles.  With this trick, makedepend won't insist on recompiling every <em>.cpp</em> file that includes <em>application.h</em> when <em>qtextedit.h</em> changes.<p> <a name="simplemain"></a><h2>A simple main()</h2><p> Let's first have a look at <em>examples/main.cpp</em>, in full ...<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> ... and go over <em>main()</em> in detail.<p> <pre>    int main( int argc, char ** argv ) {        <a href="qapplication.html">QApplication</a> a( argc, argv );</pre><p> With the above line, we create a <a href="qapplication.html">QApplication</a> object with the usual constructor and let itparse <em>argc</em> and <em>argv</em>. QApplication itself takes care of X11-specificcommand-line options like <em>-geometry</em>, thus the program automaticallybehaves the way X clients are expected to.<p> <pre>        ApplicationWindow * mw = new ApplicationWindow();        mw-&gt;<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Application" );    <a name="x2086"></a>    mw-&gt;<a href="qwidget.html#show">show</a>();</pre><p> We create an <em>ApplicationWindow</em> as a top-level widget, set its windowsystem caption to "Document 1", and <em>show()</em> it.<p> <a name="close"></a><pre>        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>()) );</pre><p> When the application's last window is closed, it should quit. Both,the signal and the slot are predefined members of <a href="qapplication.html">QApplication</a>.<p> <pre>        return a.<a href="qapplication.html#exec">exec</a>();</pre><p> Having completed the application's initialization, we start the mainevent loop (the GUI), and eventually return the error codethat QApplication returns when it leaves the event loop.<p> <pre>    }</pre><p> <a name="ApplicationWindow"></a><h2>The Implementation of ApplicationWindow</h2><p> <p> Since the implementation is quite large (almost 300 lines) we won't bore you with the preliminary headerfile <em>#includes</em>. Before we start with the constructor there are however three <em>#include</em> linesworth mentioning:<p> <pre>    #include "filesave.xpm"    #include "fileopen.xpm"    #include "fileprint.xpm"</pre><p> The tool buttons in our application wouldn't be real without icons.These icons can be found in the above xpm files. If you ever moveda program to a different location and wondered why icons were missingafterwards you will probably agree that it is a good idea to compilethem into the binary. This is what we are doing here.<p> <pre>    ApplicationWindow::ApplicationWindow()        : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose )    {</pre><p> <em>ApplicationWindow</em> inherits <a href="qmainwindow.html">QMainWindow</a>, the Qt class that providestypical application main windows, with menu bars, toolbars, etc.<p> <pre>        printer = new <a href="qprinter.html">QPrinter</a>;</pre><p> The application example can print things, and we chose to have a<a href="qprinter.html">QPrinter</a> object lying around so that when the user changes a settingduring one printing, the new setting will be the default next time.<p> <pre>        <a href="qpixmap.html">QPixmap</a> openIcon, saveIcon, printIcon;</pre><p> For simplicity reasons, our example has no more than three commandsin the toolbar.The above variables are used to hold an icon for each of them.<p> <pre>        <a href="qtoolbar.html">QToolBar</a> * fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );</pre><p> We create a toolbar in <em>this</em> window ...<p> <pre>        fileTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" );</pre><p> ... and define a title for it. When a user drags the toolbar out of its location and drops it somewhere on the desktop, the toolbar-window will show "File Operations" as caption.<p> <pre>        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" );</pre><p> Now we create the first tool button for the <em>fileTools</em> toolbarwith the appropriate icon and the tool-tip text "Open File".The <em>fileopen.xpm</em> we included at the beginning contains the definition of a pixmap named <em>fileopen</em>.This we use as the icon to illustrate our first tool button.  <p> <pre>        saveIcon = QPixmap( filesave );        QToolButton * fileSave            = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null,                               this, SLOT(save()), fileTools, "save file" );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?