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
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- extension.qdoc -->
<title>Qt 4.8: Extension Example</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Extension Example</li>
</ul>
</div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#finddialog-class-definition">FindDialog Class Definition</a></li>
<li class="level1"><a href="#finddialog-class-implementation">FindDialog Class Implementation</a></li>
</ul>
</div>
<h1 class="title">Extension Example</h1>
<span class="subtitle"></span>
<!-- $$$dialogs/extension-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="dialogs-extension-finddialog-cpp.html">dialogs/extension/finddialog.cpp</a></li>
<li><a href="dialogs-extension-finddialog-h.html">dialogs/extension/finddialog.h</a></li>
<li><a href="dialogs-extension-main-cpp.html">dialogs/extension/main.cpp</a></li>
<li><a href="dialogs-extension-extension-pro.html">dialogs/extension/extension.pro</a></li>
</ul>
<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 class="centerAlign"><img src="images/extension-example.png" alt="Screenshot of the Extension example" /></p><p>The Extension application is a dialog that allows the user to perform a simple search as well as a more advanced search.</p>
<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>
<p class="centerAlign"><img src="images/extension_more.png" alt="Screenshot of the Extension example" /></p><a name="finddialog-class-definition"></a>
<h2>FindDialog Class Definition</h2>
<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>
<pre class="cpp"> <span class="keyword">class</span> FindDialog : <span class="keyword">public</span> <span class="type"><a href="qdialog.html">QDialog</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
FindDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="keyword">private</span>:
<span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label;
<span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>lineEdit;
<span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>caseCheckBox;
<span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>fromStartCheckBox;
<span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>wholeWordsCheckBox;
<span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>searchSelectionCheckBox;
<span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>backwardCheckBox;
<span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span> <span class="operator">*</span>buttonBox;
<span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>findButton;
<span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>moreButton;
<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>extension;
};</pre>
<p>The <tt>FindDialog</tt> widget is the main application widget, and displays the application's search options and controlling buttons.</p>
<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 <a href="qcheckbox.html">QCheckBox</a>es to facilitate the search options, and we need three <a href="qpushbutton.html">QPushButton</a>s: the <b>Find</b> button to start a search and the <b>More</b> button to enable an advanced search. Finally, we need a <a href="qwidget.html">QWidget</a> representing the application's extension part.</p>
<a name="finddialog-class-implementation"></a>
<h2>FindDialog Class Implementation</h2>
<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 <a href="qcheckbox.html">QCheckBox</a>es and all the <a href="qpushbutton.html">QPushButton</a>s.</p>
<pre class="cpp"> FindDialog<span class="operator">::</span>FindDialog(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
: <span class="type"><a href="qdialog.html">QDialog</a></span>(parent)
{
label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"Find &what:"</span>));
lineEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>;
label<span class="operator">-</span><span class="operator">></span>setBuddy(lineEdit);
caseCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"Match &case"</span>));
fromStartCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"Search from &start"</span>));
fromStartCheckBox<span class="operator">-</span><span class="operator">></span>setChecked(<span class="keyword">true</span>);
findButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Find"</span>));
findButton<span class="operator">-</span><span class="operator">></span>setDefault(<span class="keyword">true</span>);
moreButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&More"</span>));
moreButton<span class="operator">-</span><span class="operator">></span>setCheckable(<span class="keyword">true</span>);</pre>
<p>We give the options and buttons a shortcut key using the & 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>
<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>
<pre class="cpp"> extension <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qwidget.html">QWidget</a></span>;
wholeWordsCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"&Whole words"</span>));
backwardCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"Search &backward"</span>));
searchSelectionCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">"Search se&lection"</span>));</pre>
<p>Then we create the extension widget, and the <a href="qcheckbox.html">QCheckBox</a>es associated with the advanced search options.</p>
<pre class="cpp"> <span class="preprocessor">#if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)</span>
<span class="comment">// Create menu</span>
<span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>menu <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qmenu.html">QMenu</a></span>(<span class="keyword">this</span>);
<span class="comment">// Create Find menu item</span>
menu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"Find"</span>));
<span class="comment">// Create More menu item</span>
<span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>moreAction <span class="operator">=</span> menu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"More"</span>));
moreAction<span class="operator">-</span><span class="operator">></span>setCheckable(<span class="keyword">true</span>);
<span class="comment">// Create Options CBA</span>
<span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>optionAction <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(tr(<span class="string">"Options"</span>)<span class="operator">,</span> <span class="keyword">this</span>);
<span class="comment">// Set defined menu into Options button</span>
optionAction<span class="operator">-</span><span class="operator">></span>setMenu(menu);
optionAction<span class="operator">-</span><span class="operator">></span>setSoftKeyRole(<span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>PositiveSoftKey);
addAction(optionAction);
<span class="comment">// Connect More menu item to setVisible slot</span>
connect(moreAction<span class="operator">,</span> SIGNAL(triggered(<span class="type">bool</span>))<span class="operator">,</span> extension<span class="operator">,</span> SLOT(setVisible(<span class="type">bool</span>)));
<span class="comment">// Create Exit CBA</span>
<span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>backSoftKeyAction <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(<span class="type"><a href="qstring.html">QString</a></span>(tr(<span class="string">"Exit"</span>))<span class="operator">,</span> <span class="keyword">this</span>);
backSoftKeyAction<span class="operator">-</span><span class="operator">></span>setSoftKeyRole(<span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>NegativeSoftKey);
<span class="comment">// Exit button closes the application</span>
connect(backSoftKeyAction<span class="operator">,</span> SIGNAL(triggered())<span class="operator">,</span> qApp<span class="operator">,</span> SLOT(quit()));
addAction(backSoftKeyAction);
<span class="preprocessor">#else</span>
buttonBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Vertical);
buttonBox<span class="operator">-</span><span class="operator">></span>addButton(findButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>ActionRole);
buttonBox<span class="operator">-</span><span class="operator">></span>addButton(moreButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>ActionRole);
connect(moreButton<span class="operator">,</span> SIGNAL(toggled(<span class="type">bool</span>))<span class="operator">,</span> extension<span class="operator">,</span> SLOT(setVisible(<span class="type">bool</span>)));
<span class="preprocessor">#endif</span>
<span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>extensionLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
extensionLayout<span class="operator">-</span><span class="operator">></span>setMargin(<span class="number">0</span>);
extensionLayout<span class="operator">-</span><span class="operator">></span>addWidget(wholeWordsCheckBox);
extensionLayout<span class="operator">-</span><span class="operator">></span>addWidget(backwardCheckBox);
extensionLayout<span class="operator">-</span><span class="operator">></span>addWidget(searchSelectionCheckBox);
extension<span class="operator">-</span><span class="operator">></span>setLayout(extensionLayout);</pre>
<p>Now that the extension widget is created, we can connect the <b>More</b> button's <a href="qabstractbutton.html#toggled">toggled()</a> signal to the extension widget's <a href="qwidget.html#visible-prop">setVisible()</a> slot.</p>
<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>
<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>
<p>We also put the check boxes associated with the advanced search options into a layout we install on the extension widget.</p>
<pre class="cpp"> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>topLeftLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>;
topLeftLayout<span class="operator">-</span><span class="operator">></span>addWidget(label);
topLeftLayout<span class="operator">-</span><span class="operator">></span>addWidget(lineEdit);
<span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>leftLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
leftLayout<span class="operator">-</span><span class="operator">></span>addLayout(topLeftLayout);
leftLayout<span class="operator">-</span><span class="operator">></span>addWidget(caseCheckBox);
leftLayout<span class="operator">-</span><span class="operator">></span>addWidget(fromStartCheckBox);
<span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>;
<span class="preprocessor">#if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_MAEMO_5) && !defined(Q_WS_SIMULATOR)</span>
mainLayout<span class="operator">-</span><span class="operator">></span>setSizeConstraint(<span class="type"><a href="qlayout.html">QLayout</a></span><span class="operator">::</span>SetFixedSize);
<span class="preprocessor">#endif</span>
mainLayout<span class="operator">-</span><span class="operator">></span>addLayout(leftLayout<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
<span class="preprocessor">#if !defined(Q_OS_SYMBIAN) && !defined(Q_WS_SIMULATOR)</span>
mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(buttonBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
<span class="preprocessor">#endif</span>
mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(extension<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
mainLayout<span class="operator">-</span><span class="operator">></span>setRowStretch(<span class="number">2</span><span class="operator">,</span> <span class="number">1</span>);
setLayout(mainLayout);
setWindowTitle(tr(<span class="string">"Extension"</span>));</pre>
<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 check boxes 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>
<pre class="cpp"> extension<span class="operator">-</span><span class="operator">></span>hide();
}</pre>
<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>
</div>
<!-- @@@dialogs/extension -->
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|