simple-application.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 618 行 · 第 1/3 页
HTML
618 行
printIcon = QPixmap( fileprint ); QToolButton * filePrint = new <a href="qtoolbutton.html">QToolButton</a>( printIcon, "Print File", QString::null, this, SLOT(print()), fileTools, "print file" );</pre><p> Likewise we create two more tool buttons in this toolbar, each with appropriate icons and tool-tip text. All three buttons are connectedto appropriate slots in this object; for example, the "Print File" button to <A HREF="#printer"><em>ApplicationWindow::print()</A></em>.<p> <pre> (void)QWhatsThis::whatsThisButton( fileTools );</pre><p> The fourth button in the toolbar is somewhat peculiar: it's the one thatprovides "What's This?" help. This must be set up using a specialfunction, as its mouse interface is different from usual.<p> <pre> 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>"; QWhatsThis::<a href="qwhatsthis.html#add">add</a>( fileOpen, fileOpenText );</pre><p> With the above line we add the "What's This?" help-text to the <em>fileOpen</em> button...<p> <pre> QMimeSourceFactory::<a href="qmimesourcefactory.html#defaultFactory">defaultFactory</a>()->setPixmap( "fileopen", openIcon );</pre><p> ... and tell the rich-text engine that when a help-text (like the onesaved in <em>fileOpenText</em>) requests an image named "fileopen", the <em>openIcon</em> pixmap is used.<p> <pre> 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 );</pre><p> The "What's This?" help of the remaining two buttons doesn't make useof pixmaps, therefore all we have to do is to add the help-text to the button.Be however careful: To invoke the rich-text elements in <em>fileSaveText</em>,the entire string must be surrounded by <p> and </p>. In <em>filePrintText</em>,we don't have rich-text elements, so this is not necessary.<p> <pre> <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 );</pre><p> Next we create a <a href="qpopupmenu.html">QPopupMenu</a> for the <em>File</em> menu and add it to the menu bar. With the ampersand previous to the letter F,we allow the user to use the shortcut <em>Alt+F</em> to open this menu.<p> <pre> file-><a href="qmenudata.html#insertItem">insertItem</a>( "&New", this, SLOT(newDoc()), CTRL+Key_N );</pre><p> Its first entry is connected to the (yet to be implementled) slot <em>newDoc()</em>. When the user chooses this <em>New</em> entry (e.g. via typing theletter N as marked by the ampersand) or uses the<em>Ctrl+N</em> accelerator, a new editor-window will pop up. <p> <pre> int id; id = file-><a href="qmenudata.html#insertItem">insertItem</a>( openIcon, "&Open...", this, SLOT(choose()), CTRL+Key_O ); 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 );</pre><p> We populate the <em>File</em> menu with three more commands (<em>Open</em>, <em>Save</em> and<em>Save</em> As), and set "What's This?" help for them. Note in particularthat "What's This?" help and pixmaps are used in both the toolbar (above)and the menu bar (here).<p> <pre> file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();</pre><p> Then we insert a separator, ... <p> <pre> 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 ); file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Quit", qApp, SLOT( <a href="qapplication.html#closeAllWindows">closeAllWindows</a>() ), CTRL+Key_Q );</pre><p> ... the <em>Print</em> command with "What's This?" help, another separator andtwo more commands (<em>Close</em> and <em>Quit</em>) without "What's This?" and pixmaps.In case of the <em>Close</em> command, the signal is connected to the <em>close()</em> slot of the respective <em>ApplicationWindow</em> object whilstthe <em>Quit</em> command affects the entire application.<p> Because <em>ApplicationWindow</em> is a <a href="qwidget.html">QWidget</a>, the <em>close()</em> functiontriggers a call to <A HREF="#closeEvent"><em>closeEvent()</A></em> which we willimplement later.<p> <A NAME="common_constructor"></A><pre> <a href="qmainwindow.html#menuBar">menuBar</a>()->insertSeparator();</pre><p> Now that we are done with the File menu we shift our focus back to themenu bar and insert a separator. From now on further menu bar entries will be aligned to the right if the windows system style suggests so.<p> <pre> <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 );</pre><p> We create a <em>Help</em> menu, add it to the menu bar, and insert a fewcommands. Depending on the style it will appear on the right hand side of the menu bar or not.<p> <pre> 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 );</pre><p> Now we create a simple text-editor, set the initial focus to it,and make it the central widget of this window.<p> <a href="qmainwindow.html#centralWidget">QMainWindow::centralWidget</a>() is the heart of the entire application:It's what menu bar, statusbar and toolbars are all arranged around. Sincethe central widget is a text editing widget, we reveal at this line thatour simple application is a text editor. :)<p> <pre> <a href="qmainwindow.html#statusBar">statusBar</a>()->message( "Ready", 2000 );</pre><p> We make the statusbar say "Ready" for two seconds at startup, just totell the user that this window has finished initialization and can beused.<p> <pre> <a href="qwidget.html#resize">resize</a>( 450, 600 );</pre><p> Finally it's time to resize the new window to a a nice default size.<p> <pre> }</pre><p> At this stage, we are done with the constructor. Among others we have learned about the classic way of creating menus and toolbars. There ishowever a more modern approach to deal with this: actions thathelp you saving some work. You mayhave a look at how the <em>ApplicationWindow</em> constructor is implementedusing <A HREF="simple-application-action.html">actions</A>. Herewe'll continue with the destructor. <p> <pre> ApplicationWindow::~ApplicationWindow() { delete printer; }</pre><p> The only thing an <em>ApplicationWindow</em> widget needs to do in its destructor is to delete theprinter it created. All other objects are child widgets, which Qtwill delete as appropriate.<p> Now our task is to implement all the slots mentioned in the header fileand used in the constructor.<p> <A NAME="newDoc()"></A><pre> void 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>(); }</pre><p> This slot, connected to the <em>File->New</em> menu item, simply creates a new <em>ApplicationWindow</em> and shows it.<p> <A NAME="choose()"></A><pre> void ApplicationWindow::choose() { <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>()->message( "Loading aborted", 2000 ); }</pre><p> The <em>choose()</em> slot is connected to the <em>Open</em> menu item andtool button. With a little help from <a href="qfiledialog.html#getOpenFileName">QFileDialog::getOpenFileName</a>(), itasks the user for a file name and then either loads that file or gives anerror message in the statusbar.<p> <pre> void ApplicationWindow::load( const <a href="qstring.html">QString</a> &fileName ) { <a href="qfile.html">QFile</a> f( fileName ); 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 ); e-><a href="qtextedit.html#setText">setText</a>( ts.<a href="qtextstream.html#read">read</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 ); }</pre><p> This function loads a file into the editor. When it's done, it sets thewindow system caption to the file name and displays a success message inthe statusbar for two seconds. With files that exist but are notreadable, nothing happens.<p> <A NAME="save()"></A><pre> void ApplicationWindow::save() { if ( filename.isEmpty() ) { saveAs(); return; } <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; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?