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"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { 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 Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main 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 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
|
---|
38 | apply 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
|
---|
84 | set data form, but we only need a single slot. Unlike the "smart" set
|
---|
85 | data form this is a "dumb" dialog that simply provides the widgets for
|
---|
86 | the caller to set and read. The caller is responsible for updating
|
---|
87 | things 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
|
---|
106 | and set an initial size.
|
---|
107 | <p> The layout of the form will be to have the chart type label and combo
|
---|
108 | box in a horizontal box layout, and similarly for the font button and
|
---|
109 | font label, and for the decimal places label and spinbox. The
|
---|
110 | buttons will also be placed in a horizontal layout, but with a spacer
|
---|
111 | to move them to the right. The show values radio buttons will be
|
---|
112 | vertically aligned within a frame. All of these will be laid out in a
|
---|
113 | vertical 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>( "&Chart Type", this );
|
---|
121 | <a name="x2631"></a> chartTypeLayout-><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-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_piechart ), "Pie Chart" );
|
---|
125 | chartTypeComboBox-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_verticalbarchart ),
|
---|
126 | "Vertical Bar Chart" );
|
---|
127 | chartTypeComboBox-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_horizontalbarchart ),
|
---|
128 | "Horizontal Bar Chart" );
|
---|
129 | chartTypeLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeComboBox );
|
---|
130 | <a name="x2630"></a> optionsFormLayout-><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
|
---|
133 | to the chart type combobox later). We also create a chart type
|
---|
134 | combobox, populating it with both pixmaps and text. We add them both
|
---|
135 | to the horizontal layout and add the horizontal layout to the form's
|
---|
136 | vertical 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>( "&Font...", this );
|
---|
140 | fontLayout-><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-><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-><a href="qboxlayout.html#addWidget">addWidget</a>( fontTextLabel );
|
---|
148 | optionsFormLayout-><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
|
---|
151 | label. The font button is straight-forward. We add a spacer to improve
|
---|
152 | the appearance. The font text label is initially empty (since we don't
|
---|
153 | know what font the user is using).
|
---|
154 | <p> <pre> addValuesFrame = new <a href="qframe.html">QFrame</a>( this );
|
---|
155 | <a name="x2640"></a> addValuesFrame-><a href="qframe.html#setFrameShape">setFrameShape</a>( QFrame::StyledPanel );
|
---|
156 | <a name="x2639"></a> addValuesFrame-><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-><a href="qgroupbox.html#setColumnLayout">setColumnLayout</a>(0, Qt::Vertical );
|
---|
161 | <a name="x2647"></a> addValuesButtonGroup-><a href="qwidget.html#layout">layout</a>()->setSpacing( 6 );
|
---|
162 | addValuesButtonGroup-><a href="qwidget.html#layout">layout</a>()->setMargin( 11 );
|
---|
163 | addValuesButtonGroupLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>(
|
---|
164 | addValuesButtonGroup-><a href="qwidget.html#layout">layout</a>() );
|
---|
165 | <a name="x2644"></a> addValuesButtonGroupLayout-><a href="qlayoutitem.html#setAlignment">setAlignment</a>( Qt::AlignTop );
|
---|
166 |
|
---|
167 | noRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&No", addValuesButtonGroup );
|
---|
168 | <a name="x2645"></a> noRadioButton-><a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );
|
---|
169 | addValuesButtonGroupLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( noRadioButton );
|
---|
170 |
|
---|
171 | yesRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&Yes", addValuesButtonGroup );
|
---|
172 | addValuesButtonGroupLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( yesRadioButton );
|
---|
173 |
|
---|
174 | asPercentageRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "As &Percentage",
|
---|
175 | addValuesButtonGroup );
|
---|
176 | addValuesButtonGroupLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( asPercentageRadioButton );
|
---|
177 | addValuesFrameLayout-><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
|
---|
180 | values 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
|
---|
182 | for them. We create a button group (so that Qt will take care of
|
---|
183 | handling the exclusive radio button behaviour automatically). Next we
|
---|
184 | create the radio buttons, making "No" the default.
|
---|
185 | <p> The decimal places label and spin box are laid out just like the other
|
---|
186 | horizontal layouts, and the buttons are laid out in a very similar way
|
---|
187 | to 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
|
---|
195 | chooseFont() slot.
|
---|
196 | <li> If the user clicks OK we call <a href="qdialog.html#accept">QDialog::accept</a>(); it is up to the
|
---|
197 | caller to read the data from the dialog's widgets and perform any
|
---|
198 | necessary 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-><a href="qlabel.html#setBuddy">setBuddy</a>( chartTypeComboBox );
|
---|
202 | decimalPlacesTextLabel-><a href="qlabel.html#setBuddy">setBuddy</a>( decimalPlacesSpinBox );
|
---|
203 | </pre>
|
---|
204 | <p> We use the setBuddy() function to associate widgets with label
|
---|
205 | accelerators.
|
---|
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>( &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
|
---|
217 | calls the static <a href="qfontdialog.html#getFont">QFontDialog::getFont</a>() function to obtain the user's
|
---|
218 | choice of font. If they chose a font we call our setFont() slot which
|
---|
219 | will 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-><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
|
---|
233 | font label and holds a copy of the font in the <tt>m_font</tt> member. We
|
---|
234 | need the font in a member so that we can provide a default font for
|
---|
235 | chooseFont().
|
---|
236 | <p> <p align="right">
|
---|
237 | <a href="tutorial2-08.html">« Taking Data</a> |
|
---|
238 | <a href="tutorial2.html">Contents</a> |
|
---|
239 | <a href="tutorial2-10.html">The Project File »</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 © 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>
|
---|