📄 simple-application.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - 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: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Walkthrough: A Simple Application</h1><br clear="all">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> - the classes that everymodern application window tends to use.<p>It further shows some use of <a href="qwhatsthis.html">QWhatsThis</a> (for simple help) and atypical main() using <a href="qapplication.html">QApplication</a>.<p>Finally, it shows a typical printout function that uses <a href="qprinter.html">QPrinter</a>.<p><h2>The declaration of ApplicationWindow</h2<p>Here's the header file in full: <pre>/****************************************************************************** $Id: qt/examples/application/application.h 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.*******************************************************************************/#ifndef APPLICATION_H#define APPLICATION_H#include <<a href="qmainwindow-h.html">qmainwindow.h</a>>class QMultiLineEdit;class QToolBar;class QPopupMenu;class ApplicationWindow: public QMainWindow{ Q_OBJECTpublic: ApplicationWindow(); ~ApplicationWindow();protected: void closeEvent( <a href="qcloseevent.html">QCloseEvent</a>* );private slots: void newDoc(); void load(); void load( const char *fileName ); void save(); void saveAs(); void print(); void about(); void aboutQt();private: <a href="qprinter.html">QPrinter</a> *printer; <a href="qmultilineedit.html">QMultiLineEdit</a> *e; <a href="qtoolbar.html">QToolBar</a> *fileTools; <a href="qstring.html">QString</a> filename;};#endif</pre><p>There's nothing much particular about this. It declares a class thatinherits QMainWindow, with some slots and some private variables. Andit uses a class predeclarations to speed up compiles: <code>QMultiLineEdit,</code> <code>QToolBar</code> and <code>QPopupMenu</code> are declared, notincluded. make depend then won't think that every .cpp file thatincludes application.h needs to be recompiled when e.g. qpopupmenu.hchanges.<p><a href="simplemain"></a><h2>The simple main()</h2<p>Here's examples/main.cpp, in full. <pre>/****************************************************************************** $Id: qt/examples/application/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 <<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#d6a291">setCaption</a>( "Qt Example - Application" ); mw-><a href="qmainwindow.html#eb53e3">show</a>(); a.<a href="qobject.html#fbde73">connect</a>( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) ); return a.<a href="qapplication.html#84c7bf">exec</a>();}</pre><p>We'll go over main() in detail. <pre> int main( int argc, char ** argv ) { <a href="qapplication.html">QApplication</a> a( argc, argv );</pre><p>We create a QApplication object with the usual constructor and let itparse <em>argc</em> and <em>argv</em> so that on X11, this program behaves as Xprograms are expected to. <pre> ApplicationWindow * mw = new ApplicationWindow(); mw-><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - Application" ); mw-><a href="qmainwindow.html#eb53e3">show</a>();</pre><p>We create an ApplicationWindow as a top-level widget, set its windowsystem caption to "Document 1", and show() it.<p><a name="close"></a> <pre> a.<a href="qobject.html#fbde73">connect</a>( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );</pre><p>When the application's last window is closed, it should quit. <pre> return a.<a href="qapplication.html#84c7bf">exec</a>();</pre><p>Having completed the application initialization, we start the mainevent loop (the GUI), and eventually return the error codeQApplication returns when it leaves the event loop.(<a href="qapplication.html#7328f6">QApplication::exit()</a> lets any part of the program set this code ina way that doesn't prohibit an orderly closedown.) <pre> }</pre><p><h2>The implementation of ApplicationWindow</h2> <p>Since the implementation is much larger (almost 200 lines) we won'tinclude it in full but instead include bits and pieces as necessary.We skip the preliminary #include and some text constants, and startwith the constructor. <pre> ApplicationWindow::ApplicationWindow() : <a href="qmainwindow.html">QMainWindow</a>( 0, "example application main window", WDestructiveClose ) {</pre><p>ApplicationWindow inherits QMainWindow, the Qt class that providestypical application main windows, with menu bars, tool bars, etc. <pre> int id; printer = new <a href="qprinter.html">QPrinter</a>;</pre><p>The application example can print things, and we chose to have aQPrinter object lying around so that when the user changes a settingduring one printing, the new setting will be the default next time. <pre> <a href="qpixmap.html">QPixmap</a> openIcon, saveIcon, printIcon;</pre><p>This example is simple enough to have just three commands. Thesevariables are used to hold the icons for each of them. <pre> fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );</pre><p>We create a tool bar in <em>this</em> window for putting some tools in. <pre> fileTools->setLabel( <a href="qobject.html#2418a9">tr</a>( "File Operations" ) ); openIcon = QPixmap( fileopen ); <a href="qtoolbutton.html">QToolButton</a> * fileOpen = new <a href="qtoolbutton.html">QToolButton</a>( openIcon, "Open File", QString::null, this, SLOT(load()), fileTools, "open file" ); saveIcon = QPixmap( filesave ); <a href="qtoolbutton.html">QToolButton</a> * fileSave = new <a href="qtoolbutton.html">QToolButton</a>( saveIcon, "Save File", QString::null, this, SLOT(save()), fileTools, "save file" ); printIcon = QPixmap( fileprint ); <a href="qtoolbutton.html">QToolButton</a> * filePrint = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null, this, SLOT(print()), fileTools, "print file" );</pre><p>And then we create three tool buttons in that tool bar, each with theappropriate icons and tool-tip text. The three buttons are connectedto slots in this object, for example the "Print File" button isconnected to ApplicationWindow::print(). <pre> (void)QWhatsThis::whatsThisButton( fileTools );</pre><p>Then we create a fourth button in the toolbar: A special button thatprovides "What's This?" help. This must be set up using a specialfunction, as its mouse interface needs to be a little unusual. <pre> <a href="qwhatsthis.html#dfcd18">QWhatsThis::add</a>( fileOpen, fileOpenText ); <a href="qmimesourcefactory.html#5c7072">QMimeSourceFactory::defaultFactory</a>()->setPixmap( "fileopen", openIcon ); <a href="qwhatsthis.html#dfcd18">QWhatsThis::add</a>( fileSave, fileSaveText ); <a href="qwhatsthis.html#dfcd18">QWhatsThis::add</a>( filePrint, filePrintText );</pre><p>We add What's This? help for each of the three buttons, and tell theQML engine that when a help text wants the "fileopen" image, it shoulduse the pixmap we've defined. <pre> <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); <a href="qmainwindow.html#0eb7bc">menuBar</a>()->insertItem( "&File", file ); file-><a href="qmenudata.html#deddb9">insertItem</a>( "&New", this, SLOT(newDoc()), CTRL+Key_N ); id = file-><a href="qmenudata.html#deddb9">insertItem</a>( openIcon, "&Open", this, SLOT(load()), CTRL+Key_O ); file-><a href="qmenudata.html#88eb55">setWhatsThis</a>( id, fileOpenText ); id = file-><a href="qmenudata.html#deddb9">insertItem</a>( saveIcon, "&Save", this, SLOT(save()), CTRL+Key_S ); file-><a href="qmenudata.html#88eb55">setWhatsThis</a>( id, fileSaveText ); id = file-><a href="qmenudata.html#deddb9">insertItem</a>( "Save &as...", this, SLOT(saveAs()) ); file-><a href="qmenudata.html#88eb55">setWhatsThis</a>( id, fileSaveText ); file-><a href="qmenudata.html#e34b79">insertSeparator</a>(); id = file-><a href="qmenudata.html#deddb9">insertItem</a>( printIcon, "&Print", this, SLOT(print()), CTRL+Key_P ); file-><a href="qmenudata.html#88eb55">setWhatsThis</a>( id, filePrintText ); file-><a href="qmenudata.html#e34b79">insertSeparator</a>(); file-><a href="qmenudata.html#deddb9">insertItem</a>( "&Close", this, SLOT(<a href="qwidget.html#3d9c87">close</a>()), CTRL+Key_W ); file-><a href="qmenudata.html#deddb9">insertItem</a>( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );</pre><p>We create a QPopupMenu item for the File menu, add it to the menu bar,populate it with three commands, and set What's This? help for them.<p>Note in particular how What's This? help and pixmaps are used in boththe toolbar (above) and the menu bar (here). <pre> <a href="qpopupmenu.html">QPopupMenu</a> * help = new <a href="qpopupmenu.html">QPopupMenu</a>( this ); <a href="qmainwindow.html#0eb7bc">menuBar</a>()->insertSeparator(); <a href="qmainwindow.html#0eb7bc">menuBar</a>()->insertItem( "&Help", help ); help-><a href="qmenudata.html#deddb9">insertItem</a>( "&About", this, SLOT(about()), Key_F1 ); help-><a href="qmenudata.html#deddb9">insertItem</a>( "About &<a href="qt.html">Qt</a>", this, SLOT(aboutQt()) ); help-><a href="qmenudata.html#e34b79">insertSeparator</a>(); help-><a href="qmenudata.html#deddb9">insertItem</a>( "What's &This", this, SLOT(<a href="qmainwindow.html#f50ce2">whatsThis</a>()), SHIFT+Key_F1 );</pre><p>We create a Help menu, add it to the menu bar, and insert a fewcommands in the Help menu. <pre> e = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this, "editor" ); e->setFocus(); <a href="qmainwindow.html#fce9ba">setCentralWidget</a>( e );</pre><p>We create a multi-line edit widget, set the initial focus to be there,and make it the central widget of this window.<p>QMainWindow::centralWidget() is the meat: It's what the menu bar,status bar and tool bars are all arranged around. Since thisapplication is an editor, the central widget is a text editing widget:) <pre> <a href="qmainwindow.html#530937">statusBar</a>()->message( "Ready", 2000 );</pre><p>We make the status bar say "Ready" for two seconds at startup, just totell the user that this window has finished initialization and can beused. <pre> <a href="qwidget.html#ff9d07">resize</a>( 450, 600 );</pre><p>We provide a nice default size for the new window. <pre> }</pre><p>That's it. <pre> ApplicationWindow::~ApplicationWindow() { delete printer; }</pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -