summaryrefslogtreecommitdiffstats
path: root/doc/html/tutorial2-09.html
blob: 5646d34909f610d7c291d317b20fd96044eed427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!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/doc/tutorial2.doc:1233 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Setting Options</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><h1 align=center>Setting Options</h1>


<p> 
<p> <center><img src="chart-options.png" alt="The options dialog"></center> 
<p> We provide an options dialog so that the user can set options that
apply to all data sets in one place.
<p> (Extracts from <tt>optionsform.h</tt>.)
<p> 

<pre>    class OptionsForm : public <a href="qdialog.html">QDialog</a>
    {
        <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
    public:
        OptionsForm( <a href="qwidget.html">QWidget</a>* parent = 0, const char* name = "options form",
                     bool modal = FALSE, WFlags f = 0 );
        ~OptionsForm() {}

        <a href="qfont.html">QFont</a> font() const { return m_font; }
        void setFont( <a href="qfont.html">QFont</a> font );

        <a href="qlabel.html">QLabel</a> *chartTypeTextLabel;
        <a href="qcombobox.html">QComboBox</a> *chartTypeComboBox;
        <a href="qpushbutton.html">QPushButton</a> *fontPushButton;
        <a href="qlabel.html">QLabel</a> *fontTextLabel;
        <a href="qframe.html">QFrame</a> *addValuesFrame;
        <a href="qbuttongroup.html">QButtonGroup</a> *addValuesButtonGroup;
        <a href="qradiobutton.html">QRadioButton</a> *noRadioButton;
        <a href="qradiobutton.html">QRadioButton</a> *yesRadioButton;
        <a href="qradiobutton.html">QRadioButton</a> *asPercentageRadioButton;
        <a href="qlabel.html">QLabel</a> *decimalPlacesTextLabel;
        <a href="qspinbox.html">QSpinBox</a> *decimalPlacesSpinBox;
        <a href="qpushbutton.html">QPushButton</a> *okPushButton;
        <a href="qpushbutton.html">QPushButton</a> *cancelPushButton;

    protected slots:
        void chooseFont();

    protected:
        <a href="qvboxlayout.html">QVBoxLayout</a> *optionsFormLayout;
        <a href="qhboxlayout.html">QHBoxLayout</a> *chartTypeLayout;
        <a href="qhboxlayout.html">QHBoxLayout</a> *fontLayout;
        <a href="qvboxlayout.html">QVBoxLayout</a> *addValuesFrameLayout;
        <a href="qvboxlayout.html">QVBoxLayout</a> *addValuesButtonGroupLayout;
        <a href="qhboxlayout.html">QHBoxLayout</a> *decimalPlacesLayout;
        <a href="qhboxlayout.html">QHBoxLayout</a> *buttonsLayout;

    private:
        <a href="qfont.html">QFont</a> m_font;
    };
</pre>
<p> The layout of this dialog is slightly more complicated than for the
set data form, but we only need a single slot. Unlike the "smart" set
data form this is a "dumb" dialog that simply provides the widgets for
the caller to set and read. The caller is responsible for updating
things based on the changes the user makes.
<p> (Extracts from <tt>optionsform.cpp</tt>.)
<p> 

<pre>    #include "images/options_horizontalbarchart.xpm"
    #include "images/options_piechart.xpm"
    #include "images/options_verticalbarchart.xpm"
</pre>
<p> We include some some pixmaps to use in the chart type combobox.
<p> <h2> The Constructor
</h2>
<a name="1"></a><p> <pre>    OptionsForm::OptionsForm( <a href="qwidget.html">QWidget</a>* parent, const char* name,
                              bool modal, WFlags f )
        : <a href="qdialog.html">QDialog</a>( parent, name, modal, f )
    {
        <a href="qwidget.html#setCaption">setCaption</a>( "Chart -- Options" );
        <a href="qwidget.html#resize">resize</a>( 320, 290 );
</pre>
<p> We pass all the arguments on to the <a href="qdialog.html">QDialog</a> constructor, set a caption
and set an initial size.
<p> The layout of the form will be to have the chart type label and combo
box in a horizontal box layout, and similarly for the font button and
font label, and for the decimal places label and spinbox. The
buttons will also be placed in a horizontal layout, but with a spacer
to move them to the right. The show values radio buttons will be
vertically aligned within a frame. All of these will be laid out in a
vertical box layout.
<p> <pre>        optionsFormLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 11, 6 );
</pre>
<p> All the widgets will be laid out within the form's vertical box layout.
<p> <pre>        chartTypeLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>( 0, 0, 6 );
</pre>
<p> The chart type label and combobox will be laid out side by side.
<p> <pre>        chartTypeTextLabel = new <a href="qlabel.html">QLabel</a>( "&amp;Chart Type", this );
    <a name="x2631"></a>    chartTypeLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeTextLabel );

        chartTypeComboBox = new <a href="qcombobox.html">QComboBox</a>( FALSE, this );
    <a name="x2633"></a>    chartTypeComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_piechart ), "Pie Chart" );
        chartTypeComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_verticalbarchart ),
                                       "Vertical Bar Chart" );
        chartTypeComboBox-&gt;<a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_horizontalbarchart ),
                                       "Horizontal Bar Chart" );
        chartTypeLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeComboBox );
    <a name="x2630"></a>    optionsFormLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( chartTypeLayout );
</pre>
<p> We create the chart type label (with an accelerator which we'll relate
to the chart type combobox later). We also create a chart type
combobox, populating it with both pixmaps and text. We add them both
to the horizontal layout and add the horizontal layout to the form's
vertical layout.
<p> <pre>        fontLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>( 0, 0, 6 );

        fontPushButton = new <a href="qpushbutton.html">QPushButton</a>( "&amp;Font...", this );
        fontLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( fontPushButton );
        <a href="qspaceritem.html">QSpacerItem</a>* spacer = new <a href="qspaceritem.html">QSpacerItem</a>( 0, 0,
                                               QSizePolicy::Expanding,
                                               QSizePolicy::Minimum );
    <a name="x2629"></a>    fontLayout-&gt;<a href="qboxlayout.html#addItem">addItem</a>( spacer );

        fontTextLabel = new <a href="qlabel.html">QLabel</a>( this ); // Must be set by caller via setFont()
        fontLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( fontTextLabel );
        optionsFormLayout-&gt;<a href="qboxlayout.html#addLayout">addLayout</a>( fontLayout );
</pre>
<p> We create a horizontal box layout to hold the font button and font
label. The font button is straight-forward. We add a spacer to improve
the appearance. The font text label is initially empty (since we don't
know what font the user is using).
<p> <pre>        addValuesFrame = new <a href="qframe.html">QFrame</a>( this );
    <a name="x2640"></a>    addValuesFrame-&gt;<a href="qframe.html#setFrameShape">setFrameShape</a>( QFrame::StyledPanel );
    <a name="x2639"></a>    addValuesFrame-&gt;<a href="qframe.html#setFrameShadow">setFrameShadow</a>( QFrame::Sunken );
        addValuesFrameLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( addValuesFrame, 11, 6 );

        addValuesButtonGroup = new <a href="qbuttongroup.html">QButtonGroup</a>( "Show Values", addValuesFrame );
    <a name="x2641"></a>    addValuesButtonGroup-&gt;<a href="qgroupbox.html#setColumnLayout">setColumnLayout</a>(0, Qt::Vertical );
    <a name="x2647"></a>    addValuesButtonGroup-&gt;<a href="qwidget.html#layout">layout</a>()-&gt;setSpacing( 6 );
        addValuesButtonGroup-&gt;<a href="qwidget.html#layout">layout</a>()-&gt;setMargin( 11 );
        addValuesButtonGroupLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>(
                                            addValuesButtonGroup-&gt;<a href="qwidget.html#layout">layout</a>() );
    <a name="x2644"></a>    addValuesButtonGroupLayout-&gt;<a href="qlayoutitem.html#setAlignment">setAlignment</a>( Qt::AlignTop );

        noRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&amp;No", addValuesButtonGroup );
    <a name="x2645"></a>    noRadioButton-&gt;<a href="qradiobutton.html#setChecked">setChecked</a>( TRUE );
        addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( noRadioButton );

        yesRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&amp;Yes", addValuesButtonGroup );
        addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( yesRadioButton );

        asPercentageRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "As &amp;Percentage",
                                                    addValuesButtonGroup );
        addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( asPercentageRadioButton );
        addValuesFrameLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( addValuesButtonGroup );
</pre>
<p> The user may opt to display their own labels as they are or to add the
values at the end of each label, either as-is or as percentages. 
<p> We create a frame to present the radio buttons in and create a layout
for them. We create a button group (so that Qt will take care of
handling the exclusive radio button behaviour automatically). Next we
create the radio buttons, making "No" the default.
<p> The decimal places label and spin box are laid out just like the other
horizontal layouts, and the buttons are laid out in a very similar way
to the buttons in the set data form.
<p> <pre>        <a href="qobject.html#connect">connect</a>( fontPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( chooseFont() ) );
        <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>() ) );
        <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>() ) );
</pre>
<p> We only need three connections:
<ol type=1>
<li> When the user clicks the font button we execute our own
chooseFont() slot.
<li> If the user clicks OK we call <a href="qdialog.html#accept">QDialog::accept</a>(); it is up to the
caller to read the data from the dialog's widgets and perform any
necessary actions.
<li> If the user clicks Cancel we call <a href="qdialog.html#reject">QDialog::reject</a>().
</ol>
<p> <pre>    <a name="x2642"></a>    chartTypeTextLabel-&gt;<a href="qlabel.html#setBuddy">setBuddy</a>( chartTypeComboBox );
        decimalPlacesTextLabel-&gt;<a href="qlabel.html#setBuddy">setBuddy</a>( decimalPlacesSpinBox );
</pre>
<p> We use the setBuddy() function to associate widgets with label
accelerators.
<p> <h2> The Slots
</h2>
<a name="2"></a><p> <pre>    void OptionsForm::chooseFont()
    {
        bool ok;
    <a name="x2638"></a>    <a href="qfont.html">QFont</a> font = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &amp;ok, m_font, this );
        if ( ok )
            <a href="qwidget.html#setFont">setFont</a>( font );
    }
</pre>
<p> When the user clicks the Font button this slot is invoked. It simply
calls the static <a href="qfontdialog.html#getFont">QFontDialog::getFont</a>() function to obtain the user's
choice of font. If they chose a font we call our setFont() slot which
will present a textual description of the font in the font label.
<p> <pre>    void OptionsForm::<a href="qwidget.html#setFont">setFont</a>( <a href="qfont.html">QFont</a> font )
    {
    <a name="x2635"></a>    <a href="qstring.html">QString</a> label = font.<a href="qfont.html#family">family</a>() + " " +
    <a name="x2637"></a>                    QString::<a href="qstring.html#number">number</a>( font.<a href="qfont.html#pointSize">pointSize</a>() ) + "pt";
    <a name="x2634"></a>    if ( font.<a href="qfont.html#bold">bold</a>() )
            label += " Bold";
    <a name="x2636"></a>    if ( font.<a href="qfont.html#italic">italic</a>() )
            label += " Italic";
        fontTextLabel-&gt;<a href="qlabel.html#setText">setText</a>( label );
        m_font = font;
    }
</pre>
<p> This function displays a textual description of the chosen font in the
font label and holds a copy of the font in the <tt>m_font</tt> member. We
need the font in a member so that we can provide a default font for
chooseFont().
<p> <p align="right">
<a href="tutorial2-08.html">&laquo; Taking Data</a> |
<a href="tutorial2.html">Contents</a> |
<a href="tutorial2-10.html">The Project File &raquo;</a>
</p>
<p> 
<!-- eof -->
<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>