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.
tqt3/doc/html/tutorial2-09.html

250 lines
14 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/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="ntqdialog.html">TQDialog</a>
{
<a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
OptionsForm( <a href="ntqwidget.html">TQWidget</a>* parent = 0, const char* name = "options form",
bool modal = FALSE, WFlags f = 0 );
~OptionsForm() {}
<a href="ntqfont.html">TQFont</a> font() const { return m_font; }
void setFont( <a href="ntqfont.html">TQFont</a> font );
<a href="ntqlabel.html">TQLabel</a> *chartTypeTextLabel;
<a href="ntqcombobox.html">TQComboBox</a> *chartTypeComboBox;
<a href="ntqpushbutton.html">TQPushButton</a> *fontPushButton;
<a href="ntqlabel.html">TQLabel</a> *fontTextLabel;
<a href="ntqframe.html">TQFrame</a> *addValuesFrame;
<a href="ntqbuttongroup.html">TQButtonGroup</a> *addValuesButtonGroup;
<a href="ntqradiobutton.html">TQRadioButton</a> *noRadioButton;
<a href="ntqradiobutton.html">TQRadioButton</a> *yesRadioButton;
<a href="ntqradiobutton.html">TQRadioButton</a> *asPercentageRadioButton;
<a href="ntqlabel.html">TQLabel</a> *decimalPlacesTextLabel;
<a href="ntqspinbox.html">TQSpinBox</a> *decimalPlacesSpinBox;
<a href="ntqpushbutton.html">TQPushButton</a> *okPushButton;
<a href="ntqpushbutton.html">TQPushButton</a> *cancelPushButton;
protected slots:
void chooseFont();
protected:
<a href="qvboxlayout.html">TQVBoxLayout</a> *optionsFormLayout;
<a href="qhboxlayout.html">TQHBoxLayout</a> *chartTypeLayout;
<a href="qhboxlayout.html">TQHBoxLayout</a> *fontLayout;
<a href="qvboxlayout.html">TQVBoxLayout</a> *addValuesFrameLayout;
<a href="qvboxlayout.html">TQVBoxLayout</a> *addValuesButtonGroupLayout;
<a href="qhboxlayout.html">TQHBoxLayout</a> *decimalPlacesLayout;
<a href="qhboxlayout.html">TQHBoxLayout</a> *buttonsLayout;
private:
<a href="ntqfont.html">TQFont</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="ntqwidget.html">TQWidget</a>* parent, const char* name,
bool modal, WFlags f )
: <a href="ntqdialog.html">TQDialog</a>( parent, name, modal, f )
{
<a href="ntqwidget.html#setCaption">setCaption</a>( "Chart -- Options" );
<a href="ntqwidget.html#resize">resize</a>( 320, 290 );
</pre>
<p> We pass all the arguments on to the <a href="ntqdialog.html">TQDialog</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">TQVBoxLayout</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">TQHBoxLayout</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="ntqlabel.html">TQLabel</a>( "&amp;Chart Type", this );
<a name="x2631"></a> chartTypeLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeTextLabel );
chartTypeComboBox = new <a href="ntqcombobox.html">TQComboBox</a>( FALSE, this );
<a name="x2633"></a> chartTypeComboBox-&gt;<a href="ntqcombobox.html#insertItem">insertItem</a>( TQPixmap( options_piechart ), "Pie Chart" );
chartTypeComboBox-&gt;<a href="ntqcombobox.html#insertItem">insertItem</a>( TQPixmap( options_verticalbarchart ),
"Vertical Bar Chart" );
chartTypeComboBox-&gt;<a href="ntqcombobox.html#insertItem">insertItem</a>( TQPixmap( 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">TQHBoxLayout</a>( 0, 0, 6 );
fontPushButton = new <a href="ntqpushbutton.html">TQPushButton</a>( "&amp;Font...", this );
fontLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( fontPushButton );
<a href="qspaceritem.html">TQSpacerItem</a>* spacer = new <a href="qspaceritem.html">TQSpacerItem</a>( 0, 0,
TQSizePolicy::Expanding,
TQSizePolicy::Minimum );
<a name="x2629"></a> fontLayout-&gt;<a href="qboxlayout.html#addItem">addItem</a>( spacer );
fontTextLabel = new <a href="ntqlabel.html">TQLabel</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="ntqframe.html">TQFrame</a>( this );
<a name="x2640"></a> addValuesFrame-&gt;<a href="ntqframe.html#setFrameShape">setFrameShape</a>( TQFrame::StyledPanel );
<a name="x2639"></a> addValuesFrame-&gt;<a href="ntqframe.html#setFrameShadow">setFrameShadow</a>( TQFrame::Sunken );
addValuesFrameLayout = new <a href="qvboxlayout.html">TQVBoxLayout</a>( addValuesFrame, 11, 6 );
addValuesButtonGroup = new <a href="ntqbuttongroup.html">TQButtonGroup</a>( "Show Values", addValuesFrame );
<a name="x2641"></a> addValuesButtonGroup-&gt;<a href="ntqgroupbox.html#setColumnLayout">setColumnLayout</a>(0, TQt::Vertical );
<a name="x2647"></a> addValuesButtonGroup-&gt;<a href="ntqwidget.html#layout">layout</a>()-&gt;setSpacing( 6 );
addValuesButtonGroup-&gt;<a href="ntqwidget.html#layout">layout</a>()-&gt;setMargin( 11 );
addValuesButtonGroupLayout = new <a href="qvboxlayout.html">TQVBoxLayout</a>(
addValuesButtonGroup-&gt;<a href="ntqwidget.html#layout">layout</a>() );
<a name="x2644"></a> addValuesButtonGroupLayout-&gt;<a href="qlayoutitem.html#setAlignment">setAlignment</a>( TQt::AlignTop );
noRadioButton = new <a href="ntqradiobutton.html">TQRadioButton</a>( "&amp;No", addValuesButtonGroup );
<a name="x2645"></a> noRadioButton-&gt;<a href="ntqradiobutton.html#setChecked">setChecked</a>( TRUE );
addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( noRadioButton );
yesRadioButton = new <a href="ntqradiobutton.html">TQRadioButton</a>( "&amp;Yes", addValuesButtonGroup );
addValuesButtonGroupLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( yesRadioButton );
asPercentageRadioButton = new <a href="ntqradiobutton.html">TQRadioButton</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 TQt 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="ntqobject.html#connect">connect</a>( fontPushButton, TQ_SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), this, TQ_SLOT( chooseFont() ) );
<a href="ntqobject.html#connect">connect</a>( okPushButton, TQ_SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), this, TQ_SLOT( <a href="ntqdialog.html#accept">accept</a>() ) );
<a href="ntqobject.html#connect">connect</a>( cancelPushButton, TQ_SIGNAL( <a href="ntqbutton.html#clicked">clicked</a>() ), this, TQ_SLOT( <a href="ntqdialog.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="ntqdialog.html#accept">TQDialog::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="ntqdialog.html#reject">TQDialog::reject</a>().
</ol>
<p> <pre> <a name="x2642"></a> chartTypeTextLabel-&gt;<a href="ntqlabel.html#setBuddy">setBuddy</a>( chartTypeComboBox );
decimalPlacesTextLabel-&gt;<a href="ntqlabel.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="ntqfont.html">TQFont</a> font = TQFontDialog::<a href="ntqfontdialog.html#getFont">getFont</a>( &amp;ok, m_font, this );
if ( ok )
<a href="ntqwidget.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="ntqfontdialog.html#getFont">TQFontDialog::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="ntqwidget.html#setFont">setFont</a>( <a href="ntqfont.html">TQFont</a> font )
{
<a name="x2635"></a> <a href="ntqstring.html">TQString</a> label = font.<a href="ntqfont.html#family">family</a>() + " " +
<a name="x2637"></a> TQString::<a href="ntqstring.html#number">number</a>( font.<a href="ntqfont.html#pointSize">pointSize</a>() ) + "pt";
<a name="x2634"></a> if ( font.<a href="ntqfont.html#bold">bold</a>() )
label += " Bold";
<a name="x2636"></a> if ( font.<a href="ntqfont.html#italic">italic</a>() )
label += " Italic";
fontTextLabel-&gt;<a href="ntqlabel.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>TQt 3.3.8</div>
</table></div></address></body>
</html>