~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/dialogs-extension.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/extension.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Extension Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Extension Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="dialogs-extension-finddialog-cpp.html">dialogs/extension/finddialog.cpp</a></li>
 
24
<li><a href="dialogs-extension-finddialog-h.html">dialogs/extension/finddialog.h</a></li>
 
25
<li><a href="dialogs-extension-main-cpp.html">dialogs/extension/main.cpp</a></li>
 
26
</ul>
 
27
<p>The Extension example shows how to add an extension to a <a href="qdialog.html">QDialog</a> using the <a href="qabstractbutton.html#toggled">QAbstractButton::toggled</a>() signal and the <a href="qwidget.html#visible-prop">QWidget::setVisible</a>() slot.</p>
 
28
<center><img src="images/extension-example.png" alt="Screenshot of the Extension example" /></center><p>The Extension application is a dialog that allows the user to perform a simple search as well as a more advanced search.</p>
 
29
<p>The simple search has two options: <b>Match case</b> and <b>Search from start</b>. The advanced search options include the possibilities to search for <b>Whole words</b>, <b>Search backward</b> and <b>Search selection</b>. Only the simple search is visible when the application starts. The advanced search options are located in the application's extension part, and can be made visible by pressing the <b>More</b> button:</p>
 
30
<center><img src="images/extension_more.png" alt="Screenshot of the Extension example" /></center><a name="finddialog-class-definition"></a>
 
31
<h2>FindDialog Class Definition</h2>
 
32
<p>The <tt>FindDialog</tt> class inherits <a href="qdialog.html">QDialog</a>. The <a href="qdialog.html">QDialog</a> class is the base class of dialog windows. A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user.</p>
 
33
<pre>&nbsp;   class FindDialog : public QDialog
 
34
    {
 
35
        Q_OBJECT
 
36
    public:
 
37
        FindDialog(QWidget *parent = 0);
 
38
 
 
39
    private:
 
40
        QLabel *label;
 
41
        QLineEdit *lineEdit;
 
42
        QCheckBox *caseCheckBox;
 
43
        QCheckBox *fromStartCheckBox;
 
44
        QCheckBox *wholeWordsCheckBox;
 
45
        QCheckBox *searchSelectionCheckBox;
 
46
        QCheckBox *backwardCheckBox;
 
47
        QPushButton *findButton;
 
48
        QPushButton *closeButton;
 
49
        QPushButton *moreButton;
 
50
        QWidget *extension;
 
51
    };</pre>
 
52
<p>The <tt>FindDialog</tt> widget is the main application widget, and displays the application's search options and controlling buttons.</p>
 
53
<p>In addition to a constructor, we declare the several child widgets: We need a <a href="qlineedit.html">QLineEdit</a> with an associated <a href="qlabel.html">QLabel</a> to let the user type a word to search for, we need several QCheckBoxes to facilitate the search options, and we need three QPushButtons: the <b>Find</b> button to start a search, the <b>More</b> button to enable an advanced search, and the <b>Close</b> button to exit the application. Finally, we need a <a href="qwidget.html">QWidget</a> representing the application's extension part.</p>
 
54
<a name="finddialog-class-implementation"></a>
 
55
<h2>FindDialog Class Implementation</h2>
 
56
<p>In the constructor we first create the standard child widgets for the simple search: the <a href="qlineedit.html">QLineEdit</a> with the associated <a href="qlabel.html">QLabel</a>, two of the QCheckBoxes and all the QPushButtons.</p>
 
57
<pre>&nbsp;   FindDialog::FindDialog(QWidget *parent)
 
58
        : QDialog(parent)
 
59
    {
 
60
        label = new QLabel(tr(&quot;Find &amp;what:&quot;));
 
61
        lineEdit = new QLineEdit;
 
62
        label-&gt;setBuddy(lineEdit);
 
63
 
 
64
        caseCheckBox = new QCheckBox(tr(&quot;Match &amp;case&quot;));
 
65
        fromStartCheckBox = new QCheckBox(tr(&quot;Search from &amp;start&quot;));
 
66
        fromStartCheckBox-&gt;setChecked(true);
 
67
 
 
68
        findButton = new QPushButton(tr(&quot;&amp;Find&quot;));
 
69
        findButton-&gt;setDefault(true);
 
70
 
 
71
        closeButton = new QPushButton(tr(&quot;Close&quot;));
 
72
 
 
73
        moreButton = new QPushButton(tr(&quot;&amp;More&quot;));
 
74
        moreButton-&gt;setCheckable(true);</pre>
 
75
<p>We give the options and buttons a shortcut key using the &amp; character. In the <b>Find what</b> option's case, we also need to use the <a href="qlabel.html#setBuddy">QLabel::setBuddy</a>() function to make the shortcut key work as expected; then, when the user presses the shortcut key indicated by the label, the keyboard focus is transferred to the label's buddy widget, the <a href="qlineedit.html">QLineEdit</a>.</p>
 
76
<p>We set the <b>Find</b> button's default property to true, using the <a href="qpushbutton.html#default-prop">QPushButton::setDefault</a>() function. Then the push button will be pressed if the user presses the Enter (or Return) key. Note that a <a href="qdialog.html">QDialog</a> can only have one default button.</p>
 
77
<pre>&nbsp;       extension = new QWidget;
 
78
 
 
79
        wholeWordsCheckBox = new QCheckBox(tr(&quot;&amp;Whole words&quot;));
 
80
        backwardCheckBox = new QCheckBox(tr(&quot;Search &amp;backward&quot;));
 
81
        searchSelectionCheckBox = new QCheckBox(tr(&quot;Search se&amp;lection&quot;));</pre>
 
82
<p>Then we create the extension widget, and the QCheckBoxes associated with the advanced search options.</p>
 
83
<pre>&nbsp;       connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
 
84
        connect(moreButton, SIGNAL(toggled(bool)), extension, SLOT(setVisible(bool)));
 
85
 
 
86
        QVBoxLayout *extensionLayout = new QVBoxLayout;
 
87
        extensionLayout-&gt;setMargin(0);
 
88
        extensionLayout-&gt;addWidget(wholeWordsCheckBox);
 
89
        extensionLayout-&gt;addWidget(backwardCheckBox);
 
90
        extensionLayout-&gt;addWidget(searchSelectionCheckBox);
 
91
        extension-&gt;setLayout(extensionLayout);</pre>
 
92
<p>Now that the extension widget is created, we can connect the <b>More</b> button's <tt>toggled()</tt> signal to the extension widget's <tt>setVisible()</tt> slot.</p>
 
93
<p>The <a href="qabstractbutton.html#toggled">QAbstractButton::toggled</a>() signal is emitted whenever a checkable button changes its state. The signal's argument is true if the button is checked, or false if the button is unchecked. The <a href="qwidget.html#visible-prop">QWidget::setVisible</a>() slot sets the widget's visible status. If the status is true the widget is shown, otherwise the widget is hidden.</p>
 
94
<p>Since we made the <b>More</b> button checkable when we created it, the connection makes sure that the extension widget is shown depending on the state of <b>More</b> button.</p>
 
95
<p>We also connect the <b>Close</b> button to the <a href="qwidget.html#close">QWidget::close</a>() slot, and we put the checkboxes associated with the advanced search options into a layout we install on the extension widget.</p>
 
96
<pre>&nbsp;       QHBoxLayout *topLeftLayout = new QHBoxLayout;
 
97
        topLeftLayout-&gt;addWidget(label);
 
98
        topLeftLayout-&gt;addWidget(lineEdit);
 
99
 
 
100
        QVBoxLayout *leftLayout = new QVBoxLayout;
 
101
        leftLayout-&gt;addLayout(topLeftLayout);
 
102
        leftLayout-&gt;addWidget(caseCheckBox);
 
103
        leftLayout-&gt;addWidget(fromStartCheckBox);
 
104
        leftLayout-&gt;addStretch(1);
 
105
 
 
106
        QVBoxLayout *rightLayout = new QVBoxLayout;
 
107
        rightLayout-&gt;addWidget(findButton);
 
108
        rightLayout-&gt;addWidget(closeButton);
 
109
        rightLayout-&gt;addWidget(moreButton);
 
110
        rightLayout-&gt;addStretch(1);
 
111
 
 
112
        QGridLayout *mainLayout = new QGridLayout;
 
113
        mainLayout-&gt;setSizeConstraint(QLayout::SetFixedSize);
 
114
        mainLayout-&gt;addLayout(leftLayout, 0, 0);
 
115
        mainLayout-&gt;addLayout(rightLayout, 0, 1);
 
116
        mainLayout-&gt;addWidget(extension, 1, 0, 1, 2);
 
117
        setLayout(mainLayout);
 
118
 
 
119
        setWindowTitle(tr(&quot;Extension&quot;));</pre>
 
120
<p>Before we create the main layout, we create several child layouts for the widgets: First we allign the <a href="qlabel.html">QLabel</a> ans its buddy, the <a href="qlineedit.html">QLineEdit</a>, using a <a href="qhboxlayout.html">QHBoxLayout</a>. Then we vertically allign the <a href="qlabel.html">QLabel</a> and <a href="qlineedit.html">QLineEdit</a> with the checkboxes associated with the simple search, using a <a href="qvboxlayout.html">QVBoxLayout</a>. We also create a <a href="qvboxlayout.html">QVBoxLayout</a> for the buttons. In the end we lay out the two latter layouts and the extension widget using a <a href="qgridlayout.html">QGridLayout</a>.</p>
 
121
<pre>&nbsp;       extension-&gt;hide();
 
122
    }</pre>
 
123
<p>Finally, we hide the extension widget using the <a href="qwidget.html#hide">QWidget::hide</a>() function, making the application only show the simple search options when it starts. When the user wants to access the advanced search options, the dialog only needs to change the visibility of the extension widget. Qt's layout management takes care of the dialog's appearance.</p>
 
124
<p /><address><hr /><div align="center">
 
125
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
126
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
127
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
128
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
129
</tr></table></div></address></body>
 
130
</html>