You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
qt3/doc/html/designer-manual-2.html

295 lines
24 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/tools/designer/book/chap-quickstart2.leaf:3 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Quick Start</title>
<style type="text/css"><!--
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><p align="right">[<a href="designer-manual-1.html">Prev: Preface</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-3.html">Next: Creating a Main Window Application</a>]</p>
<h2 align="center">Quick Start</h2>
<p>This chapter provides a quick start for users of <em>Qt Designer</em>. The chapter takes you step-by-step through the creation of a small dialog-style metric conversion application. It introduces many of the common tasks users perform when creating an application: adding widgets to a form, setting widget properties, making connections between signals and slots, laying out widgets, and adding custom code. This chapter only covers a portion of <em>Qt Designer</em>'s functionality; explanations and details are mostly left for the more detailed <tt>colortool</tt> tutorial presented in chapters two and three.</p>
<p align="center"><img align="middle" src="qsmetric.png" width="328" height="239">
</p>
<blockquote><p align="center"><em>The Metric Conversion Dialog</em></p></blockquote>
<h3><a name="1"></a>Starting and Exiting <em>Qt Designer</em></h3>
<h4><a name="1-1"></a>Starting <em>Qt Designer</em> under Windows</h4>
<p>To start <em>Qt Designer</em> under Windows, click the <b>Start</b> button then select <b>Programs|Qt X.x.x|Designer</b>. (X.x.x is the Qt version number, e.g. 3.3.1.)</p>
<h4><a name="1-2"></a>Starting <em>Qt Designer</em> under Unix or Linux</h4>
<p><em>Qt Designer</em> may be started in a number of ways, depending on the desktop environment you are using:</p>
<ul><li><p>If there is a <em>Qt Designer</em> icon on the desktop background, you may double click this to start <em>Qt Designer</em>.</p>
<li><p>For desktop environments which feature a <b>Start</b> menu, there may be a <em>Qt Designer</em> item in the relevant submenu; try looking for a <em>Qt Designer</em> entry in the "Programming" or "Development" submenus, and select this.</p>
<li><p>Entering <tt>designer</tt> in a terminal window will also start <em>Qt Designer</em>.</p>
</ul><h4><a name="1-3"></a>Starting <em>Qt Designer</em> under Mac OS X</h4>
<p>Double click on <em>Qt Designer</em> in the Finder.</p>
<h4><a name="1-4"></a>Exiting</h4>
<p>When you have finished using <em>Qt Designer</em>, just click <b>File|Exit</b>. If you have unsaved work, <em>Qt Designer</em> will ask you whether you wish to save or discard any changes.</p>
<h3><a name="2"></a>Creating the Project</h3>
<p>Start <em>Qt Designer</em> now. When <em>Qt Designer</em> starts, it shows the <em>New/Open</em> dialog. We will open this dialog ourselves, so click <b>Cancel</b> to close it.</p>
<p>Our metric conversion application is a standard C++ application, so we must create a C++ project, and add our files and code to this project.</p>
<p>Create the new project as follows:</p>
<ul><li><p>Click <b>File|New</b> to invoke the <em>New File</em> dialog.</p>
<li><p>Click "C++ Project" to create a C++ project then click <b>OK</b> to pop up the <em>Project Settings</em> dialog.</p>
<li><p>Click the <b>ellipsis (...)</b> button next to the Project File line edit to invoke the <em>Save As</em> dialog. Use this dialog to navigate to where you want to create the new project, creating a new folder for it (called "metric") using the <b>Create New Folder</b> toolbar button.</p>
<li><p>Enter a filename of "metric.pro" then click <b>Save</b>; the project's name will now be "metric".</p>
<li><p>Click <b>OK</b> to close the <em>Project Settings</em> dialog.</p>
<li><p>Click <b>File|Save</b> to save the project.</p>
</ul><p align="center"><img align="middle" src="qs-projset.png" width="383" height="273">
</p>
<p>See also <a href="designer-manual-3.html#creating-the-project">Creating the Project</a>.</p>
<h3><a name="3"></a>Creating the Dialog</h3>
<ul><li><p>Click <b>File|New</b> to invoke the <em>New File</em> dialog.</p>
<li><p>Click "Dialog" then click <b>OK</b>.</p>
<li><p>Drag a corner of the new form to make it a lot smaller.</p>
<li><p>Change the form's name (in the <a href="designer-manual-3.html#using-the-property-editor-sidebar">Property Editor</a>) to "ConversionForm", and change its caption to "Metric Conversion".</p>
<li><p>Click <b>File|Save</b>, accept the default form name, and click <b>Save</b> to save it.</p>
</ul><p align="center"><img align="middle" src="qs-newdlg.png" width="395" height="211">
</p>
<p>See also <a href="designer-manual-4.html">Creating Dialogs</a> and <a href="designer-manual-4.html#layouts-subsection">Layouts</a>.</p>
<h4><a name="3-1"></a>Adding Widgets to the Dialog</h4>
<h5><a name="3-1-1"></a>Adding Text Labels</h5>
<ul><li><p>Click the <b>Common Widgets</b> button listed in the Toolbox toolbar at the left of <em>Qt Designer</em>'s main window.</p>
<li><p>Double click the <b>TextLabel</b> button then click inside the dialog near the top left corner.</p>
<li><p>Click four more times below the top text label until you have five labels from top to bottom. Don't worry about exact placement of the labels since the layout tools will handle this automatically.</p>
<li><p>Click the <b>Pointer</b> toolbar button to deselect the text label.</p>
</ul><p>See also <a href="designer-manual-4.html#adding-the-widgets-subsection">Adding the Widgets</a>.</p>
<p>We will change the properties of the text labels to suit the application. This will make them easier to refer to later.</p>
<ul><li><p>Click on the text label that is closest to the top of the dialog. Go to the <em>Property Editor</em> window, and change the label's <em>text</em> property to "Enter &amp;Number:". The &amp; (ampersand) makes the following letter an Alt key accelerator (keyboard shortcuts).</p>
<li><p>Click the second label. Change the label's <em>text</em> property to "Convert &amp;From:".</p>
<li><p>Click the third label. Change the label's <em>text</em> property to "Convert &amp;To:".</p>
<li><p>Click the fourth label. Change the label's <em>text</em> property to "Result:".</p>
<li><p>Click the fifth label. Change the label's <em>text</em> property to "&amp;Decimals:".</p>
</ul><p align="center"><img align="middle" src="qs-txtlbl.png" width="424" height="212">
</p>
<p>See also <a href="designer-manual-3.html#using-the-property-editor-sidebar">Using the Property Editor</a>.</p>
<h5><a name="3-1-2"></a>Adding Line Edits, Comboboxes, and Spinboxes</h5>
<ul><li><p>Click the <b>Common Widgets</b> button listed in the Toolbox toolbar at the left of <em>Qt Designer</em>'s main window.</p>
<li><p>Click the <b>LineEdit</b> button and then click to the right of the Enter Number label. Change its <em>name</em> property to "numberLineEdit". Change the <em>hAlign</em> property to AlignRight and change <em>vAlign</em> to AlignTop. Change the <em>font</em> property to use a bold variant by expanding it and adjusting the value in its <em>bold</em> subsection. Finally, change <em>wordwrap</em> to False. Note that whenever you click a widget, its properties appear in the <em>Property Editor</em>.</p>
<li><p>Double click the <b>ComboBox</b> button, and click to the right of the Convert From label. Now click to the right of the "Convert To" label. Click the <b>Pointer</b> toolbar button to deselect the ComboBox. Change the <em>name</em> property of the first combobox to "fromComboBox". Change the <em>name</em> property of the second to "toComboBox".</p>
<li><p>Click the <b>LineEdit</b> button then click to the right of the "Result" label. Change its <em>name</em> property to "resultLineEdit". Change the <em>paletteBackgroundColor</em> property to a shade of yellow and modify the <em>font</em> property to use a bold variant (just to make the result stand out). Change <em>hAlign</em> to AlignRight, <em>vAlign</em> to AlignVCenter, and <em>wordwrap</em> to False. Now change the <em>readOnly</em> property to "True".</p>
<li><p>Click the <b>SpinBox</b> button then click to the right of the Decimals label. Place the SpinBox below the right side of the resultLineEdit. Change the spinbox's <em>name</em> property to "decimalsSpinBox". Change its <em>max value</em> property to "6", and the <em>value</em> to "3".</p>
<li><p>Click <b>File|Save</b>.</p>
</ul><p align="center"><img align="middle" src="qs-addwidg.png" width="422" height="210">
</p>
<p>Now we need to relate each of the text labels to the corresponding widget. We do this by creating "buddies".</p>
<p>A widget that does not accept focus itself, e.g. a <a href="qlabel.html">QLabel</a>, can have an accelerator that will pass the focus to its "buddy", e.g. a <a href="qlineedit.html">QLineEdit</a>. In <em>Qt Designer</em>, we enable this by setting the first widget's <em>buddy</em> property to the name of the buddy widget.</p>
<ul><li><p>Click the <b>Set Buddy</b> toolbar button (or press <b>F12</b>). Click the Enter Number label, drag the line to the numberLineEdit, then release.</p>
<li><p>Click the <b>Set Buddy</b> toolbar button. Click the Convert From label, drag the line to the fromComboBox, then release.</p>
<li><p>Click the <b>Set Buddy</b> toolbar button. Click the Convert To label, drag the line to the toComboBox, then release.</p>
<li><p>Click the <b>Set Buddy</b> toolbar button. Click the Decimals label, drag the line to the decimalsSpinBox, then release.</p>
</ul><h5><a name="3-1-3"></a>Adding Push Buttons</h5>
<ul><li><p>Click the <b>Common Widgets</b> button in the Toolbox.</p>
<li><p>Double click the <b>Pushbutton</b> button then click inside the dialog near the bottom left corner, just below the "Decimals" label.</p>
<li><p>Click twice more to the right of the new push button, so that you have three push buttons in a horizontal row at the bottom of the dialog.</p>
<li><p>Click the <b>Pointer</b> toolbar button to deselect the push button.</p>
</ul><p>We will change a few properties for each of the push buttons in the <em>Property Editor</em> window.</p>
<ul><li><p>Click the left-most push button, and change its name to "clearPushButton", and its text to "&amp;Clear".</p>
<li><p>Click the middle push button and change its name to "calculatePushButton", its text to "Calculate", and its default property to "True".</p>
<li><p>Click the right-most push button, change its name to "quitPushButton", and change its text to "&amp;Quit".</p>
</ul><p align="center"><img align="middle" src="qs-pushbutt.png" width="428" height="232">
</p>
<h5><a name="3-1-4"></a>Adding Spacers</h5>
<p>We need to add spacers to absorb redundant space in our dialog, so that it will lay out nicely at any size. Usually spacers are added as you experiment with the layout but, since this is a quick guide to <em>Qt Designer</em>, and we already know that they will be needed, we will add the spacers now.</p>
<ul><li><p>Click the <b>Common Widgets</b> button in the Toolbox.</p>
<li><p>Click the <b>Spacer</b> button. Click to the right of the "Decimal" label, drag right towards the spinbox, then release. A horizontal spacer will appear.</p>
<li><p>Click the <b>Spacer</b> button again. Click to the right of the Calculate push button, drag right towards the Quit push button, then release.</p>
<li><p>Click the <b>Spacer</b> button. Click just below the spin box, drag vertically towards the buttons, then release. This time, a vertical spacer will appear.</p>
</ul><p align="center"><img align="middle" src="qs-spacers.png" width="434" height="243">
</p>
<p>Click <b>File|Save</b> to save the dialog.</p>
<h5><a name="3-1-5"></a>Editing Widgets</h5>
<p>We will edit some of the widgets to contain the values needed for the conversions.</p>
<p>The fromComboBox:</p>
<ul><li><p>Right click the fromComboBox then click <b>Edit</b> from the context menu.</p>
<li><p>Click <b>New Item</b>, and replace the "New Item" text with "Kilometers".</p>
<li><p>Click <b>New Item</b>, and change the text to "Meters".</p>
<li><p>Click <b>New Item</b>, and change the text to "Centimeters".</p>
<li><p>Click <b>New Item</b>, and change the text to "Millimeters".</p>
<li><p>Click <b>OK</b> to close the Edit Listbox dialog.</p>
</ul><p align="center"><img align="middle" src="qs-editlistbox1.png" width="490" height="255">
</p>
<p>The toComboBox:</p>
<ul><li><p>Right click the toComboBox then click <b>Edit</b> from the context menu.</p>
<li><p>Delete the "New Item" text, and type "Miles".</p>
<li><p>Click <b>New Item</b>, and change the text to "Yards".</p>
<li><p>Click <b>New Item</b>, and change the text to "Feet".</p>
<li><p>Click <b>New Item</b>, and change the text to "Inches".</p>
<li><p>Click <b>OK</b> to close the Edit Listbox dialog.</p>
</ul><h4><a name="3-2"></a>Laying Out the Dialog</h4>
<p>We will lay out the text labels with their corresponding widgets first, and lay out the push buttons last.</p>
<ul><li><p>Click the decimalsSpinBox, and press <b>Shift+Click</b> on the spacer next to it.</p>
<li><p>Click the <b>Lay Out Horizontally</b> (<b>Ctrl+H</b>) toolbar button.</p>
<li><p>Click the form to deselect anything that's selected.</p>
<li><p>Click the form to the left of the Decimals text label, drag the rubber band so that all the widgets above the vertical spacer are selected, then release.</p>
<li><p>Click the <b>Lay Out in a Grid</b> (<b>Ctrl+G</b>) toolbar button.</p>
<p align="center"><img align="middle" src="qs-laygrid.png" width="396" height="243">
</p>
<li><p>Click the Clear push button in the <em>Object Explorer</em> window (Objects tab) then <b>Shift+Click</b> the Calculate push button, the Quit push button, and the Spacer between the push buttons.</p>
<li><p>Click the <b>Lay Out Horizontally</b> toolbar button.</p>
<p align="center"><img align="middle" src="qs-layhoriz.png" width="387" height="260">
</p>
<li><p>Click the form to deselect all the widgets and layouts then click the <b>Lay Out Vertically</b> (<b>Ctrl+L</b>) toolbar button.</p>
<li><p>Finally, click the form then click the <b>Adjust Size</b> (<b>Ctrl+J</b>) toolbar button.</p>
</ul><p align="center"><img align="middle" src="qs-layvert.png" width="388" height="258">
</p>
<p>Click <b>File|Save</b> to save the dialog.</p>
<p>See also <a href="designer-manual-4.html#laying-out-the-widgets-subsection">Laying Out the Widgets</a>.</p>
<h5><a name="3-2-1"></a>Tab Order</h5>
<p>We should make sure that our dialog's tab order is set correctly.</p>
<ul><li><p>Click the <b>Tab Order</b> toolbar button. A number in a blue circle will appear next to every widget that can accept the keyboard focus.</p>
<li><p>Click every widget in turn in the order you want them to accept the keyboard focus.</p>
<li><p>Press <b>Esc</b> to leave tab order mode.</p>
</ul><p align="center"><img align="middle" src="qs-taborder.png" width="324" height="253">
</p>
<p>See also <a href="designer-manual-4.html#changing-the-tab-order-sidebar">Changing the Tab Order</a>.</p>
<h4><a name="3-3"></a>Previewing the Dialog</h4>
<p>To preview the dialog, press <b>Ctrl+T</b> or click <b>Preview|Preview Form</b> from the menubar. Try dragging the corner of the dialog to adjust the size. Note that the the widgets always stay in proportion no matter what size you make the dialog. Check the tab order of the widgets by pressing the <b>Tab</b> key.</p>
<h4><a name="3-4"></a>Connecting the Widgets</h4>
<p>We need to connect three buttons: the Clear button, the Calculate button, and the Quit button. We also need to connect some of the other widgets. For convenience, we can make all of our connections at once using the <em>View and Edit Connections</em> dialog.</p>
<p>We will now connect our clearButton:</p>
<ul><li><p>Click <b>Edit|Connections</b> to invoke the <em>View and Edit Connections</em> dialog.</p>
<li><p>Click <b>New</b> to enter a new connection.</p>
<li><p>For the first connection, select clearPushButton for the Sender, clicked() for the Signal, numberLineEdit for the Receiver, and clear() for the Slot.</p>
<li><p>Click <b>New</b> again.</p>
<li><p>Select clearPushButton for the Sender, clicked() for the Signal, resultLineEdit for the Receiver, and clear() for the Slot.</p>
<li><p>Click <b>New</b> again.</p>
<li><p>Select clearPushButton for the Sender, clicked() for the Signal, numberLineEdit for the Receiver, and setFocus() for the Slot.</p>
</ul><p align="center"><img align="middle" src="qs-editconn1.png" width="608" height="391">
</p>
<p>We also need to connect the quit button to the form:</p>
<ul><li><p>Click <b>New</b>.</p>
<li><p>Select quitPushButton for Sender, clicked() for Signal, ConversionForm for Receiver, and close() for Slot.</p>
</ul><p>We want to connect the calculate button and the other widgets, but the slot we want to use is not listed in the combobox. We will need to create a new slot so that we can select it from the list to complete our connections.</p>
<ul><li><p>Click <b>New</b>.</p>
<li><p>Select calculatePushButton for the Sender, clicked() for the Signal, and ConversionForm for the Receiver.</p>
<li><p>Click <b>Edit Slots</b> to invoke the <em>Edit Functions</em> dialog.</p>
<li><p>Click <b>New Function</b>.</p>
<li><p>Type over the Function name with the name of our new function: convert().</p>
<li><p>Click <b>OK</b>.</p>
<li><p>Select convert() from the Slot combobox to complete our connection.</p>
</ul><p align="center"><img align="middle" src="qs-editfunc.png" width="608" height="397">
</p>
<p>We will now connect the last few widgets:</p>
<ul><li><p>Click <b>New</b>.</p>
<li><p>Select decimalsSpinBox as the Sender, valueChanged(int) as the Signal, ConversionForm as the Receiver, and convert() as the Slot.</p>
<li><p>Click <b>New</b>.</p>
<li><p>Select fromComboBox as the Sender, activated(int) as the Signal, ConversionForm as the Receiver, and convert() as the Slot.</p>
<li><p>Click <b>New</b>.</p>
<li><p>Select toComboBox as the Sender, activated(int) as the Signal, ConversionForm as the Receiver, and convert() as the Slot.</p>
<li><p>Click <b>New</b>.</p>
<li><p>Select calculatePushButton as the Sender, clicked() as the Signal, numberLineEdit as the Receiver, and setFocus() as the Slot.</p>
</ul><p align="center"><img align="middle" src="qs-editconn2.png" width="608" height="391">
</p>
<p>Click <b>OK</b> to exit the View and Edit Connections dialog.</p>
<p>Click <b>Save</b> to save the project.</p>
<h3><a name="4"></a>Coding the Dialog</h3>
<p>Click "conversionform.ui.h" in the <em>Project Overview</em> window to invoke the code editor. We will implement the <tt>convert()</tt> and <tt>init()</tt> functions. For faster implementation, copy the code from this section and then follow the brief explanations below:</p>
<pre> void ConversionForm::convert()
{
enum MetricUnits {
Kilometers,
Meters,
Centimeters,
Millimeters
};
enum OldUnits {
Miles,
Yards,
Feet,
Inches
};
// Retrieve the input
double input = numberLineEdit-&gt;text().toDouble();
double scaledInput = input;
// internally convert the input to millimeters
switch ( fromComboBox-&gt;currentItem() ) {
case Kilometers:
scaledInput *= 1000000;
break;
case Meters:
scaledInput *= 1000;
break;
case Centimeters:
scaledInput *= 10;
break;
}
//convert to inches
double result = scaledInput * 0.0393701;
switch ( toComboBox-&gt;currentItem() ) {
case Miles:
result /= 63360;
break;
case Yards:
result /= 36;
break;
case Feet:
result /= 12;
break;
}
// set the result
int decimals = decimalsSpinBox-&gt;value();
resultLineEdit-&gt;setText( QString::<a href="qstring.html#number">number</a>( result, 'f', decimals ) );
numberLineEdit-&gt;setText( QString::<a href="qstring.html#number">number</a>( input, 'f', decimals ) );
}
</pre>
<p>First, we define some enums for the input and output units. Then we retrieve the input from the numberLineEdit. We convert the input to millimeters because this is the most precise metric unit we support. Then we convert it to inches which is the most precise output unit we support. We then scale it to the selected output unit. Finally, we put the result in the resultLineEdit.</p>
<p>Next, we will implement the <tt>init()</tt> function which is called when the dialog is created.</p>
<pre> void ConversionForm::init()
{
numberLineEdit-&gt;setValidator( new <a href="qdoublevalidator.html">QDoubleValidator</a>( numberLineEdit ) );
numberLineEdit-&gt;setText( "10" );
convert();
numberLineEdit-&gt;selectAll();
}
</pre>
<p>For this function, we set a validator on the numberLineEdit so that the user can only input numbers. To be able to do this, we also need to add <tt>#include &lt;qvalidator.h&gt;</tt> at the top of the "conversionform.ui.h" file, before the <tt>init()</tt> function. Lastly, we set the initial input.</p>
<p>We are almost ready to run the application. Before we compile the application, we need a <tt>main.cpp</tt> file.</p>
<ul><li><p>Click <b>File|New</b> to open the New File dialog.</p>
<li><p>Click C++ Main File (<tt>main.cpp</tt>) then click <b>OK</b>.</p>
<li><p>Accept the default selections for the Configure Main-File dialog.</p>
<li><p>Click <b>Save</b> to save the project.</p>
</ul><p>Compiling and Running the Application</p>
<ul><li><p>Start or switch to a console, and navigate to the directory where the project is saved.</p>
<li><p>Run <em>qmake</em> on the project file (<tt>metric.pro</tt>) by typing <tt>qmake -o Makefile metric.pro</tt>.</p>
<li><p>Run <em>make</em> (or <em>nmake</em> depending on your system).</p>
<li><p>After the project has compiled, run the application.</p>
</ul><h3><a name="5"></a>Wrapping Up</h3>
<p>In this brief introduction to <em>Qt Designer</em>, we have covered the basic tasks involved in creating a simple dialog-style application. The user should now be able to add widgets to a form, set widget properties, connect signals and slots, lay out the form, and add custom code. We have ignored many of the details, and deferred some explanations until later chapters. The next two chapters provide a tutorial that covers the development of a small but complete application, and the reference chapters complete the detailed coverage.</p>
<!-- eof -->
<p align="right">[<a href="designer-manual-1.html">Prev: Preface</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-3.html">Next: Creating a Main Window Application</a>]</p>
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>