~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/designer-designing-a-component.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/designer-manual.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Designing a Component with Qt Designer</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
    <link rel="prev" href="designer-getting-started.html" />
 
15
    <link rel="contents" href="designer-manual.html" />
 
16
    <link rel="next" href="designer-editing-mode.html" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
22
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
24
[Previous: <a href="designer-getting-started.html">Getting Started with Qt Designer</a>]
 
25
[<a href="designer-manual.html">Contents</a>]
 
26
[Next: <a href="designer-editing-mode.html">Qt Designer's Form Editing Mode</a>]
 
27
</p>
 
28
<h1 align="center">Designing a Component with Qt Designer</h1>
 
29
<ul><li><a href="#creating-a-dialog">Creating a Dialog</a></li>
 
30
<li><a href="#composing-the-dialog">Composing the Dialog</a></li>
 
31
<li><a href="#creating-a-layout">Creating a Layout</a></li>
 
32
<li><a href="#signal-and-slot-connections">Signal and Slot Connections</a></li>
 
33
</ul>
 
34
<p>In this chapter we will look at the main steps that users will take when creating new graphical user interfaces with <i>Qt Designer</i>. Usually, creating a new component for an application will involve various activities:</p>
 
35
<ul>
 
36
<li>Deciding which kind of component to use.</li>
 
37
<li>Composing the user interface by adding widgets from the tool panel to the form.</li>
 
38
<li>Laying out the contents of the form.</li>
 
39
<li>Connecting widgets together with signals and slots by using the connection editor.</li>
 
40
<li>Previewing the form.</li>
 
41
</ul>
 
42
<p>Users may find that they they prefer to perform these activities in a different order, and we expect that for complex widgets the design process may involve many iterations of these steps. However, we present each of the activities in the above order, and leave it up to the user to find the approach that suits them best.</p>
 
43
<p>To demonstrate the processes used to create a new component, we will take a look at the steps needed to create a simple dialog with <i>Qt Designer</i>. We use the dialog to illustrate certain features of the tool. General concepts are explained in the gray boxes in each section, and you can follow links from some of these to read more about the features being discussed.</p>
 
44
<a name="creatingadialog"></a><a name="creating-a-dialog"></a>
 
45
<h2>Creating a Dialog</h2>
 
46
<p><i>Qt Designer</i> can be used to create user interfaces for different purposes, and provides different kinds of forms for each of these. The first choice that you face when creating a new user interface is to decide which kind of form you will be using; for example, you may require a dialog rather than a generic custom widget. New forms can be created by opening the <b>File</b> menu and selecting the <b>New Form...</b> option, or by pressing <b>Ctrl+N</b>.</p>
 
47
<a name="choosingaform"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
48
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-choosing-form.png" /></td><td><b>Choosing a form</b><p><i>Qt Designer</i> provides a number of template forms that are suitable for different kinds of components. You can either choose one of these templates or open an form that was saved earlier.</p>
 
49
</td></tr>
 
50
</table>
 
51
<p>If your requirements later change so that you need to supply a different type of component, you can easily reuse the contents of your existing form. For our example, we select the <b>Dialog</b> template, and an empty form is created ready for use.</p>
 
52
<a name="composingthedialog"></a><a name="composing-the-dialog"></a>
 
53
<h2>Composing the Dialog</h2>
 
54
<p>The empty form is displayed as an empty dialog with a grid pattern overlayed on the widget area in a style similar to many drawing packages. <i>Qt Designer</i> provides two different editing modes: In form editing mode, widgets and other objects can be arranged, resized, and configured on the form; in connection editing mode, signals and slots connections can be made between the widgets.</p>
 
55
<p>We ensure that <i>Qt Designer</i> is in form editing mode by opening the <b>Form</b> menu and selecting <b>Edit Widgets</b>. This makes it possible for widgets to be dragged from the main toolbox onto the form.</p>
 
56
<a name="editingmodes"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
57
<tr valign="top" bgcolor="#a2c511"><th></th><th><b>Editing modes</b></th></tr>
 
58
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-widget-tool.png" /></td><td>In <a href="designer-editing-mode.html">form editing mode</a>, we can change the appearance of the form, add layouts, and edit the properties of each widget.</td></tr>
 
59
<tr valign="top" bgcolor="#e0e0e0"><td><img src="images/designer-connection-tool.png" /></td><td>In <a href="designer-connection-mode.html">connection editing mode</a>, we can connect widgets together using Qt's signals and slots mechanism.</td></tr>
 
60
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-buddy-tool.png" /></td><td>In <a href="designer-buddy-mode.html">Qt Designer's Buddy Editing Mode</a>, buddy widgets can be assigned to label widgets to help them handle keyboard focus correctly.</td></tr>
 
61
<tr valign="top" bgcolor="#e0e0e0"><td><img src="images/designer-tab-order-tool.png" /></td><td>In <a href="designer-tab-order.html">tab order mode</a>, we can set the order in which widgets receive the keyboard focus.</td></tr>
 
62
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-resource-tool.png" /></td><td>In <a href="designer-resources.html">resource editing mode</a>, resources can be associated with a particular form, making it easier to use images and other external data with widgets.</td></tr>
 
63
</table>
 
64
<p>For our example, we will develop a dialog that lets the user create an image with a given size and color depth - these properties can be represented by spinboxes and a combobox. We will also provide a line edit so that the user can specify a name for the image if they want.</p>
 
65
<p>Before we begin adding widgets to the form, it is useful to resize it to give us the required amount of space to work with. We can make the form larger than necessary because it can be resized later.</p>
 
66
<p>We start composing the dialog by placing some text labels onto the form. This is achieved by dragging each <i>Label</i> from the <i>Display</i> section of the toolbox onto the form. We add some spinboxes and a combobox from the <i>Input</i> section of the toolbox, one vertical spacer and one horizontal spacer from the <i>Spacers</i> section (represented on the form as blue springs), and two push buttons from the <i>Buttons</i> section.</p>
 
67
<p>Widgets and spacers can be moved around on the form by dragging them to their new locations. A widget can be resized by dragging the resize handles that appear when it is selected - click the left mouse button over the widget to select it. It can be helpful to resize label widgets so that all of their text is displayed, but this is not necessary as they will be displayed fully when we add them to a layout. It is useful to place the widgets where they are needed on the form because it helps visualize how it will appear when it is finished, but we do not need to fine tune the positions of widgets - this task is the responsibility of the form's layout objects which we will add later.</p>
 
68
<a name="addingwidgetstoaform"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
69
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-dialog-initial.png" /></td><td><b>Adding widgets to a form</b><p>The dialog initially contains standard widgets and spacers. All the objects on the form can be freely positioned. The spacers have no effect since they have not yet been included in any layouts.</p>
 
70
</td></tr>
 
71
</table>
 
72
<p>The label text and initial values are changed in the Property Editor. This can be accessed by opening the <i>Tools</i> menu in <i>Qt Designer</i>'s main window and selecting <i>Property Editor</i>, or by pressing <b>Ctrl+I</b>. The Property Editor always shows the properties of the currently selected object, so we just need to select each of the widgets in turn to access their properties.</p>
 
73
<p>For the labels and push buttons, we only need to change the <tt>text</tt> property of each widget; for the spinboxes, we must change the <tt>value</tt>, <tt>minimum</tt>, and <tt>maximum</tt> properties. The dialog's window title can also be changed using the Property Editor - click on the form itself to access the dialog's properties.</p>
 
74
<p>It is usually a good idea to give meaningful names to widgets in case you want to connect their signals to slots in other components, or refer to the widgets by name in your application.</p>
 
75
<a name="thepropertyeditor"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
76
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-property-editor-link.png" /></td><td><b>The Property Editor</b><p>All designable widget properties can be edited in the <a href="designer-editing-mode.html">Property Editor</a> window.</p>
 
77
<p>The properties can be edited by using the controls provided; the type of control supplied for each property depends on the type of data it contains. Any changes made to the widget's properties cause it to be updated immediately.</p>
 
78
</td></tr>
 
79
</table>
 
80
<p>Once all the widgets are in position, they need to be placed into one or more layouts. These will ensure that the widgets are the correct size, and are correctly positioned on the form.</p>
 
81
<a name="creatingalayout"></a><a name="creating-a-layout"></a>
 
82
<h2>Creating a Layout</h2>
 
83
<p>In Qt, widget positions and sizes are organized by layout managers. Before a form can be used, it is necessary to put each of the widgets into layouts, and set the form's overall layout. <i>Qt Designer</i> provides all the standard types of layout, making it simple to experiment with different ways of arranging components.</p>
 
84
<p>At any point during editing, we can experiment with the layout of widgets on the form by selecting a group of objects and applying a layout to them. Any of the standard Qt layouts (horizontal, vertical, and grid) can be used. To apply a layout to a selection of objects, click the appropriate icon in <i>Qt Designer</i>'s tool bar, or select the layout from the form's context menu.</p>
 
85
<p>We begin laying out our dialog by putting all the labels and their associated input widgets into a grid layout. To do this, we need to ensure that all the widgets are lined up in a grid arrangement. We put the push buttons into a horizontal layout with the horizontal spacer.</p>
 
86
<a name="placingwidgetsinlayouts"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
87
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-dialog-layout.png" /></td><td><b>Placing widgets in layouts</b><p>The widgets and spacers are placed in a grid layout that takes up as little space as possible. The horizontal spacer in the lower layout reserves some space to the left of the push buttons, and makes the layout larger than normal. This will ensure that the buttons are positioned on the right hand side of the dialog.</p>
 
88
<p>We will examine the use of layouts in more detail in <a href="designer-editing-mode.html">Form Editing Mode</a>.</p>
 
89
</td></tr>
 
90
</table>
 
91
<p>The Property Editor can be used to change the appearance of layouts on the form. To access the properties of a layout, we click inside the layout, taking care not to click any of the widgets. The <tt>margin</tt> property controls the thickness of the margin around the edges of the layout, and <tt>spacing</tt> determines the space between widgets. By default, <i>Qt Designer</i> gives inner layouts a 1 pixel wide <tt>margin</tt> so that it can show a border around each object on the form. If you wish, you can set this to 0.</p>
 
92
<p>We can create more complex layouts by selecting the layouts and placing them into other layouts, but we really only need a form-level vertical layout to ensure that the dialog will appear correctly. To do this, we click the form to ensure that no objects are selected then apply the vertical layout. The form may now be larger than necessary, but we can resize it to make it smaller.</p>
 
93
<center><img src="images/designer-dialog-final.png" /></center><p>Once all the objects on the form have been placed in layouts, we can concentrate on connecting them together using Qt's signals and slots mechanism.</p>
 
94
<a name="signalandslotconnections"></a><a name="signal-and-slot-connections"></a>
 
95
<h2>Signal and Slot Connections</h2>
 
96
<p>Objects on the form can be connected together using their signals and slots, making it unnecessary to write code in your application to connect many standard widgets together. In addition, <i>Qt Designer</i>'s connection editor provides a visual representation of the signal and slot connections in the form that can help other developers understand how the final component will behave.</p>
 
97
<p>We can show how the connection editor is used by connecting the <a href="qlineedit.html#returnPressed">returnPressed()</a> signal of the line edit in our dialog to the <b>OK</b> button's <a href="qabstractbutton.html#animateClick">animateClick()</a> slot. We change into connection editing mode by opening the <b>Edit</b> menu and selecting <b>Edit Signals/Slots</b>. The objects in the form will now become highlighted as the cursor passes over them.</p>
 
98
<a name="connectingwidgets"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
99
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-making-connection.png" /></td><td><b>Connecting widgets</b><p>To connect the line edit to the <b>OK</b> button, we click on the widget and drag the cursor toward the push button; a line extends from the line edit to the cursor.</p>
 
100
</td></tr>
 
101
</table>
 
102
<p>When the cursor is over the <b>OK</b> button, we release the mouse button to complete the connection, and a dialog opens to allow us to choose compatible signals and slots. Since the returnPressed() signal and the animateClick() slot have compatible signatures, we can use them together to &quot;click&quot; the <b>OK</b> button whenever the return key is pressed in the line edit. To try this out, we can open a preview of the dialog by pressing <b>Ctrl+R</b> or by opening the <b>Form</b> menu and selecting <b>Preview</b>.</p>
 
103
<a name="previewingaform"></a><table align="center" cellpadding="2" cellspacing="1" border="0">
 
104
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/designer-dialog-preview.png" /></td><td><b>Previewing a form</b><p>When you preview a form, it appears just as it would in your application. You can resize it to see how the layouts used to manage the child widgets respond to different window sizes, and use this information to fine tune the form's design.</p>
 
105
</td></tr>
 
106
</table>
 
107
<p>
 
108
[Previous: <a href="designer-getting-started.html">Getting Started with Qt Designer</a>]
 
109
[<a href="designer-manual.html">Contents</a>]
 
110
[Next: <a href="designer-editing-mode.html">Qt Designer's Form Editing Mode</a>]
 
111
</p>
 
112
<p /><address><hr /><div align="center">
 
113
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
114
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
115
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
116
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
117
</tr></table></div></address></body>
 
118
</html>