designer-manual-3.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 337 行 · 第 1/4 页
HTML
337 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/tools/designer/book/chap-main-windows.leaf:3 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Creating Main Windows with Actions, Toolbars and Menus</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><p align="right">[<a href="designer-manual-2.html">Prev: Creating a Qt Application</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-4.html">Next: The Designer Approach</a>]</p><h2 align="center">Creating Main Windows with Actions, Toolbars and Menus</h2><!-- index Actions and Action Groups --><!-- index Main Window!Creating --><!-- index Creating Main Windows --><!-- index Creating Menus --><!-- index Menus!Adding --><!-- index Creating Toolbars --><!-- index Toolbars, Creating --><!-- index Toolbar Buttons!Adding --><!-- index Adding!Menus --><!-- index Adding!Toolbars --><!-- index Adding!Actions and Action Groups --><!-- index Adding!Toolbar Buttons --><p>In this chapter we will explain how to create an application's main window and how to add actions (explained shortly), menus and toolbars. We will also demonstrate how some common actions, like cut and paste in a <b>QTextEdit</b>, can be performed automatically simply by making the appropriate signals and slots connections. We will build the <em>richedit</em> application to illustrate the necessary techniques.</p><p align="center"><img align="middle" src="richedit-main.png" width="739" height="383"></p><blockquote><p align="center"><em>The Richedit Application</em></p></blockquote><!-- index Projects --><p>We begin by creating a project file. Start <em>Qt Designer</em> or if it is already running, close any existing projects and files. Click <b>File|New</b> to invoke the <em>New File</em> dialog. Click the 'C++ Project' icon, then click <b>OK</b> to invoke the <em>Project Settings</em> dialog. Click the ellipsis button to invoke the <em>Save As</em> dialog and navigate to where you want to put the new project. Use the <b>Create New Folder</b> toolbar button to create the 'richedit' directory if it doesn't exist. Make sure you're in the 'richedit' directory (double click it) and enter a file name of 'richedit.pro'. Click the <b>Save</b> button. The 'Project File' field of the <em>Project Settings</em> dialog will have the path and name of your new project; click <b>OK</b> to create the project.</p><p>If you're unfamiliar with Actions and Action Groups the sidebar provides the necessary introduction; otherwise skip ahead to "Designing the Main Window".</p><blockquote><p align="center"><b> Actions and Action Groups</b></p><p>An <em>action</em> is an operation that the user initiates through the user interface, for example, saving a file or changing some text's font weight to bold.</p><p>We often want the user to be able to perform an action using a variety of means. For example, to save a file we might want the user to be able to press <b>Ctrl+S</b>, or to click the <b>Save</b> toolbar button or to click the <b>File|Save</b> menu option. Although the means of invoking the action are all different, the underlying operation is the same and we don't want to duplicate the code that performs the operation. In Qt we can create an action (a <b>QAction</b> object) which will call the appropriate function when the action is invoked. We can assign an accelerator, (e.g. <b>Ctrl+S</b>), to an action. We can also add an action to a menu item and to a toolbar button.</p><p>If the action has an on/off state, e.g. bold is on or off, when the user changes the state, for example by clicking a toolbar button, the state of everything associated with the action, e.g. menu items and toolbar buttons, are updated.</p><p>Some actions should operate together like radio buttons. For example, if we have left align, center align and right align actions, only one should be 'on' at any one time. An <em>action group</em> (a <b>QActionGroup</b> object) is used to group a set of actions together. If the action group's <tt>exclusive</tt> property is TRUE then only one of the actions in the group can be on at any one time. If the user changes the state of an action in an action group where <tt>exclusive</tt> is TRUE, everything associated with the actions in the action group, e.g. menu items and toolbar buttons, are updated.</p><!-- index Actions and Action Groups --><p><em>Qt Designer</em> can create actions and action groups visually, can assign accelerators to them, and can associate them with menu items and toolbar buttons.</p></blockquote><h3><a name="1"></a>Designing the Main Window</h3><!-- index Main Window!Creating --><!-- index Creating Main Windows --><!-- index Main Window!Wizard --><!-- index Wizards!Main Window --><!-- index Creating Menus --><!-- index Menus!Adding --><!-- index Adding!Menus --><!-- index Creating Toolbars --><!-- index Toolbars, Creating --><!-- index Toolbar Buttons!Adding --><!-- index Adding!Toolbars --><!-- index Adding!Actions and Action Groups --><!-- index Adding!Toolbar Buttons --><p>We will use the main window wizard to build a main window. The wizard allows us to create actions and a menu bar and a toolbar through which the user can invoke the actions. We will also create our own actions, menus and toolbar. We will add some widgets to the toolbar and add a main widget to the main window. Finally we will connect signals to slots to take advantage of Qt's default functionality and minimize our coding.</p><h4><a name="1-1"></a>Creating the Main Window</h4><p>Click <b>File|New</b> to invoke the <em>New File</em> dialog, click Mainwindow, then click <b>OK</b>. A new <b>QMainWindow</b> form will be created and the Mainwindow Wizard will pop up.</p><ol type=1><li><p>The <em>Choose available menus and toolbars</em> wizard page appears first. It presents three categories of default actions, File Actions, Edit Actions and Help Actions. For each category you can choose to have <em>Qt Designer</em> create menu items, toolbar buttons and signal/slots connections for the relevant actions. You can always add or delete actions, menu items, toolbar buttons and connections later.</p><!-- index Creating Menus --><!-- index Menus!Adding --><!-- index Creating Toolbars --><!-- index Toolbars, Creating --><!-- index Toolbar Buttons!Adding --><!-- index Adding!Menus --><!-- index Adding!Toolbars --><!-- index Adding!Actions and Action Groups --><!-- index Adding!Toolbar Buttons --><!-- index Signals and Slots!Connecting Actions --><p>We will accept the defaults for File Actions, i.e. have menu items, toolbar buttons and the relevant connections created. But for the Edit Actions we don't want any connections created since we'll be connecting the actions directly to the <b>QTextEdit</b> we'll create later, so uncheck the Edit Action's Create Slots and Connections checkbox. We won't have any Help Actions on the toolbar so uncheck the Help Action's Toolbar checkbox. Click <b>Next</b> to move on to the next wizard page.</p><p align="center"><img align="middle" src="qd-chapmainwin-mainwinwiz1.png" width="612" height="430"></p><blockquote><p align="center"><em>Main Window Wizard- Choosing menus and toolbars</em></p></blockquote><li><!-- index Creating Toolbars --><!-- index Toolbars, Creating --><!-- index Toolbar Buttons!Adding --><!-- index Adding!Toolbar Buttons --><!-- index Separator!Menu item --><!-- index Separator!Toolbar button --><p>The <em>Setup Toolbar</em> wizard page is used to populate a toolbar with actions from each of the default action categories. The Category combobox is used to select which set of actions you wish to work on. The Actions list box lists the actions available for the current category. The Toolbar listbox lists the toolbar buttons you want to create. The blue left and right arrow buttons are used to move actions into or out of the Toolbar list box. The blue up and down arrow buttons are used to move actions up and down within the Toolbar list box. Note that the '<Separator>' item in the Actions list box may be moved to the Toolbar list box as often as required and will cause a separator to appear in the finished toolbar.</p><p>Copy the New, Open and Save Actions to the Toolbar list box. Copy a <Separator> to the Toolbar list box. Change the Category to Edit and copy the Undo, Redo, Cut Copy and Paste actions to the Toolbar list box. Click <b>Next</b> and then click <b>Finish</b>.</p><p align="center"><img align="middle" src="qd-chapmainwin-mainwinwiz2.png" width="612" height="430"></p><blockquote><p align="center"><em>Main Window Wizard- Setting up the toolbar</em></p></blockquote></ol><p>If you preview the form (<b>Ctrl+T</b>) the File and Edit menus will be available and you'll be able to drag the toolbar either into an independent window of its own, or to dock it to the left, right, bottom or top of the window. The menus and toolbars are not functional yet but we will rectify this as we progress.</p><p align="center"><img align="middle" src="qd-chapmainwin-mainwinwiz3.png" width="608" height="508"></p><blockquote><p align="center"><em>Previewing the Form</em></p></blockquote><p>Click <b>File|Save</b> to save the form as <tt>richedit.ui</tt>.</p><h4><a name="1-2"></a>Creating and Deleting Actions and Action Groups</h4><h5><a name="1-2-1"></a>Creating Actions</h5><!-- index Actions and Action Groups!Adding Actions --><!-- index Adding!Actions and Action Groups --><p>Our application requires more menu items and toolbar buttons than the the defaults we created with the main window wizard. But before we create the menu items and toolbar buttons we will create the actions that they'll invoke. The Action Editor appears automatically when you create a main window. You can also access it through the Window menu (click <b>Window|Action Editor</b>).</p><p align="center"><img align="middle" src="qd-chapmainwin-actioneditor1.png" width="200" height="242"></p><blockquote><p align="center"><em>Action Editor</em></p></blockquote><p>For the richedit application we need to create actions for indicating bold, italic and underlined font attributes, and to set text alignment.</p><p>Right click in the <em>Action Editor</em> where the actions are listed, then left click New Action. This will create a new action called 'Action' at the top of the list of actions. The Property Editor will change to show the default settings for the new action. We'll now go through all the properties we need to change.</p><ol type=1><li><p>Change the <em>name</em> of the action to 'boldAction'.</p><li><p>Since bold can only be on or off change the <em>toggleAction</em> property to True.</p><li><!-- index Adding!Pixmaps --><!-- index Pixmaps!Adding --><p>The <em>iconSet</em> property is used to set an icon which will appear to the left of the action's name in any menu you associate the action with. The <em>iconSet</em> is also used for the toolbar button if you associate the action with a toolbar. Click the ellipsis button (<b>...</b>) to invoke the <em>Choose an Image</em> dialog. The ellipsis button appears when you click in the Value part of the Properties list by a <em>pixmap</em> or <em>iconSet</em> property. The pixmap we require is not in the default collection. Click the <b>Add</b> button and a file dialog will appear. The icons we require are in the Qt examples directory, <tt>qt/examples/textedit/</tt>. Navigate to the textedit directory and select the <tt>textbold.xpm</tt> file and then click <b>Open</b>. Click the new textbold icon in the pixmap collection then click <b>OK</b>.</p><p align="center"><img align="middle" src="qd-chapmainwin-actioneditor2.png" width="200" height="242"></p><blockquote><p align="center"><em>Properties of Bold Action</em></p></blockquote><li><p>Change the <em>text</em> property to 'bold'. This automatically changes the <em>menuText</em>, <em>toolTip</em> and <em>statusTip</em> properties.</p><li><p>Change the menu text to '&Bold'. If we choose to associate this action with a menu item then this property is used; otherwise it is ignored.</p><li><p>Change the <em>accel</em> property to 'CTRL+B'. This will associate the <b>Ctrl+B</b> keyboard accelerator with this action.</p><p align="center"><img align="middle" src="qd-chapmainwin-propeditnewact.png" width="217" height="351"></p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?