source: trunk/doc/html/tutorial2-09.html@ 203

Last change on this file since 203 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 14.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial2.doc:1233 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Setting Options</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Setting Options</h1>
33
34
35<p>
36<p> <center><img src="chart-options.png" alt="The options dialog"></center>
37<p> We provide an options dialog so that the user can set options that
38apply to all data sets in one place.
39<p> (Extracts from <tt>optionsform.h</tt>.)
40<p>
41
42<pre> class OptionsForm : public <a href="qdialog.html">QDialog</a>
43 {
44 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
45 public:
46 OptionsForm( <a href="qwidget.html">QWidget</a>* parent = 0, const char* name = "options form",
47 bool modal = FALSE, WFlags f = 0 );
48 ~OptionsForm() {}
49
50 <a href="qfont.html">QFont</a> font() const { return m_font; }
51 void setFont( <a href="qfont.html">QFont</a> font );
52
53 <a href="qlabel.html">QLabel</a> *chartTypeTextLabel;
54 <a href="qcombobox.html">QComboBox</a> *chartTypeComboBox;
55 <a href="qpushbutton.html">QPushButton</a> *fontPushButton;
56 <a href="qlabel.html">QLabel</a> *fontTextLabel;
57 <a href="qframe.html">QFrame</a> *addValuesFrame;
58 <a href="qbuttongroup.html">QButtonGroup</a> *addValuesButtonGroup;
59 <a href="qradiobutton.html">QRadioButton</a> *noRadioButton;
60 <a href="qradiobutton.html">QRadioButton</a> *yesRadioButton;
61 <a href="qradiobutton.html">QRadioButton</a> *asPercentageRadioButton;
62 <a href="qlabel.html">QLabel</a> *decimalPlacesTextLabel;
63 <a href="qspinbox.html">QSpinBox</a> *decimalPlacesSpinBox;
64 <a href="qpushbutton.html">QPushButton</a> *okPushButton;
65 <a href="qpushbutton.html">QPushButton</a> *cancelPushButton;
66
67 protected slots:
68 void chooseFont();
69
70 protected:
71 <a href="qvboxlayout.html">QVBoxLayout</a> *optionsFormLayout;
72 <a href="qhboxlayout.html">QHBoxLayout</a> *chartTypeLayout;
73 <a href="qhboxlayout.html">QHBoxLayout</a> *fontLayout;
74 <a href="qvboxlayout.html">QVBoxLayout</a> *addValuesFrameLayout;
75 <a href="qvboxlayout.html">QVBoxLayout</a> *addValuesButtonGroupLayout;
76 <a href="qhboxlayout.html">QHBoxLayout</a> *decimalPlacesLayout;
77 <a href="qhboxlayout.html">QHBoxLayout</a> *buttonsLayout;
78
79 private:
80 <a href="qfont.html">QFont</a> m_font;
81 };
82</pre>
83<p> The layout of this dialog is slightly more complicated than for the
84set data form, but we only need a single slot. Unlike the "smart" set
85data form this is a "dumb" dialog that simply provides the widgets for
86the caller to set and read. The caller is responsible for updating
87things based on the changes the user makes.
88<p> (Extracts from <tt>optionsform.cpp</tt>.)
89<p>
90
91<pre> #include "images/options_horizontalbarchart.xpm"
92 #include "images/options_piechart.xpm"
93 #include "images/options_verticalbarchart.xpm"
94</pre>
95<p> We include some some pixmaps to use in the chart type combobox.
96<p> <h2> The Constructor
97</h2>
98<a name="1"></a><p> <pre> OptionsForm::OptionsForm( <a href="qwidget.html">QWidget</a>* parent, const char* name,
99 bool modal, WFlags f )
100 : <a href="qdialog.html">QDialog</a>( parent, name, modal, f )
101 {
102 <a href="qwidget.html#setCaption">setCaption</a>( "Chart -- Options" );
103 <a href="qwidget.html#resize">resize</a>( 320, 290 );
104</pre>
105<p> We pass all the arguments on to the <a href="qdialog.html">QDialog</a> constructor, set a caption
106and set an initial size.
107<p> The layout of the form will be to have the chart type label and combo
108box in a horizontal box layout, and similarly for the font button and
109font label, and for the decimal places label and spinbox. The
110buttons will also be placed in a horizontal layout, but with a spacer
111to move them to the right. The show values radio buttons will be
112vertically aligned within a frame. All of these will be laid out in a
113vertical box layout.
114<p> <pre> optionsFormLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 11, 6 );
115</pre>
116<p> All the widgets will be laid out within the form's vertical box layout.
117<p> <pre> chartTypeLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>( 0, 0, 6 );
118</pre>
119<p> The chart type label and combobox will be laid out side by side.
120<p> <pre> chartTypeTextLabel = new <a href="qlabel.html">QLabel</a>( "&amp;Chart Type", this );
121 <a name="x2631"></a> chartTypeLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeTextLabel );
122
123 chartTypeComboBox = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
124 <a name="x2633"></a> chartTypeComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_piechart ), "Pie Chart" );
125 chartTypeComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_verticalbarchart ),
126 "Vertical Bar Chart" );
127 chartTypeComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_horizontalbarchart ),
128 "Horizontal Bar Chart" );
129 chartTypeLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeComboBox );
130 <a name="x2630"></a> optionsFormLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( chartTypeLayout );
131</pre>
132<p> We create the chart type label (with an accelerator which we'll relate
133to the chart type combobox later). We also create a chart type
134combobox, populating it with both pixmaps and text. We add them both
135to the horizontal layout and add the horizontal layout to the form's
136vertical layout.
137<p> <pre> fontLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>( 0, 0, 6 );
138
139 fontPushButton = new <a href="qpushbutton.html">QPushButton</a>( "&amp;Font...", this );
140 fontLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( fontPushButton );
141 <a href="qspaceritem.html">QSpacerItem</a>* spacer = new <a href="qspaceritem.html">QSpacerItem</a>( 0, 0,
142 QSizePolicy::Expanding,
143 QSizePolicy::Minimum );
144 <a name="x2629"></a> fontLayout-&gt;<a href="qboxlayout.html#addItem">addItem</a>( spacer );
145
146 fontTextLabel = new <a href="qlabel.html">QLabel</a>( this ); // Must be set by caller via setFont()
147 fontLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( fontTextLabel );
148 optionsFormLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( fontLayout );
149</pre>
150<p> We create a horizontal box layout to hold the font button and font
151label. The font button is straight-forward. We add a spacer to improve
152the appearance. The font text label is initially empty (since we don't
153know what font the user is using).
154<p> <pre> addValuesFrame = new <a href="qframe.html">QFrame</a>( this );
155 <a name="x2640"></a> addValuesFrame-&gt;<a href="qframe.html#setFrameShape">setFrameShape</a>( QFrame::StyledPanel );
156 <a name="x2639"></a> addValuesFrame-&gt;<a href="qframe.html#setFrameShadow">setFrameShadow</a>( QFrame::Sunken );
157 addValuesFrameLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( addValuesFrame, 11, 6 );
158
159 addValuesButtonGroup = new <a href="qbuttongroup.html">QButtonGroup</a>( "Show Values", addValuesFrame );
160 <a name="x2641"></a> addValuesButtonGroup-&gt;<a href="qgroupbox.html#setColumnLayout">setColumnLayout</a>(0, Qt::Vertical );
161 <a name="x2647"></a> addValuesButtonGroup-&gt;<a href="qwidget.html#layout">layout</a>()-&gt;setSpacing( 6 );
162 addValuesButtonGroup-&gt;<a href="qwidget.html#layout">layout</a>()-&gt;setMargin( 11 );
163 addValuesButtonGroupLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>(
164 addValuesButtonGroup-&gt;<a href="qwidget.html#layout">layout</a>() );
165 <a name="x2644"></a> addValuesButtonGroupLayout-&gt;<a href="qlayoutitem.html#setAlignment">setAlignment</a>( Qt::AlignTop );
166
167 noRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&amp;No", addValuesButtonGroup );
168 <a name="x2645"></a> noRadioButton-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );
169 addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( noRadioButton );
170
171 yesRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&amp;Yes", addValuesButtonGroup );
172 addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( yesRadioButton );
173
174 asPercentageRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "As &amp;Percentage",
175 addValuesButtonGroup );
176 addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( asPercentageRadioButton );
177 addValuesFrameLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( addValuesButtonGroup );
178</pre>
179<p> The user may opt to display their own labels as they are or to add the
180values at the end of each label, either as-is or as percentages.
181<p> We create a frame to present the radio buttons in and create a layout
182for them. We create a button group (so that Qt will take care of
183handling the exclusive radio button behaviour automatically). Next we
184create the radio buttons, making "No" the default.
185<p> The decimal places label and spin box are laid out just like the other
186horizontal layouts, and the buttons are laid out in a very similar way
187to the buttons in the set data form.
188<p> <pre> <a href="qobject.html#connect">connect</a>( fontPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( chooseFont() ) );
189 <a href="qobject.html#connect">connect</a>( okPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( <a href="qdialog.html#accept">accept</a>() ) );
190 <a href="qobject.html#connect">connect</a>( cancelPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( <a href="qdialog.html#reject">reject</a>() ) );
191</pre>
192<p> We only need three connections:
193<ol type=1>
194<li> When the user clicks the font button we execute our own
195chooseFont() slot.
196<li> If the user clicks OK we call <a href="qdialog.html#accept">QDialog::accept</a>(); it is up to the
197caller to read the data from the dialog's widgets and perform any
198necessary actions.
199<li> If the user clicks Cancel we call <a href="qdialog.html#reject">QDialog::reject</a>().
200</ol>
201<p> <pre> <a name="x2642"></a> chartTypeTextLabel-&gt;<a href="qlabel.html#setBuddy">setBuddy</a>( chartTypeComboBox );
202 decimalPlacesTextLabel-&gt;<a href="qlabel.html#setBuddy">setBuddy</a>( decimalPlacesSpinBox );
203</pre>
204<p> We use the setBuddy() function to associate widgets with label
205accelerators.
206<p> <h2> The Slots
207</h2>
208<a name="2"></a><p> <pre> void OptionsForm::chooseFont()
209 {
210 bool ok;
211 <a name="x2638"></a> <a href="qfont.html">QFont</a> font = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &amp;ok, m_font, this );
212 if ( ok )
213 <a href="qwidget.html#setFont">setFont</a>( font );
214 }
215</pre>
216<p> When the user clicks the Font button this slot is invoked. It simply
217calls the static <a href="qfontdialog.html#getFont">QFontDialog::getFont</a>() function to obtain the user's
218choice of font. If they chose a font we call our setFont() slot which
219will present a textual description of the font in the font label.
220<p> <pre> void OptionsForm::<a href="qwidget.html#setFont">setFont</a>( <a href="qfont.html">QFont</a> font )
221 {
222 <a name="x2635"></a> <a href="qstring.html">QString</a> label = font.<a href="qfont.html#family">family</a>() + " " +
223 <a name="x2637"></a> QString::<a href="qstring.html#number">number</a>( font.<a href="qfont.html#pointSize">pointSize</a>() ) + "pt";
224 <a name="x2634"></a> if ( font.<a href="qfont.html#bold">bold</a>() )
225 label += " Bold";
226 <a name="x2636"></a> if ( font.<a href="qfont.html#italic">italic</a>() )
227 label += " Italic";
228 fontTextLabel-&gt;<a href="qlabel.html#setText">setText</a>( label );
229 m_font = font;
230 }
231</pre>
232<p> This function displays a textual description of the chosen font in the
233font label and holds a copy of the font in the <tt>m_font</tt> member. We
234need the font in a member so that we can provide a default font for
235chooseFont().
236<p> <p align="right">
237<a href="tutorial2-08.html">&laquo; Taking Data</a> |
238<a href="tutorial2.html">Contents</a> |
239<a href="tutorial2-10.html">The Project File &raquo;</a>
240</p>
241<p>
242<!-- eof -->
243<p><address><hr><div align=center>
244<table width=100% cellspacing=0 border=0><tr>
245<td>Copyright &copy; 2007
246<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
247<td align=right><div align=right>Qt 3.3.8</div>
248</table></div></address></body>
249</html>
Note: See TracBrowser for help on using the repository browser.