Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > dbf69eaa13c86c67c80d31d9cbb7847c > files > 3738

qtbase5-doc-5.9.4-1.2.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- extension.qdoc -->
  <title>Extension Example | Qt Widgets 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-dialogs.html">Dialog Examples</a></td><td >Extension Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<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>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Extension Example</h1>
<span class="subtitle"></span>
<!-- $$$dialogs/extension-description -->
<div class="descr"> <a name="details"></a>
<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 id="finddialog-class-definition">FindDialog Class Definition</h2>
<p>The <code>FindDialog</code> 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 <code>FindDialog</code> 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 id="finddialog-class-implementation">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">&quot;Find &amp;what:&quot;</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">&gt;</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">&quot;Match &amp;case&quot;</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">&quot;Search from &amp;start&quot;</span>));
      fromStartCheckBox<span class="operator">-</span><span class="operator">&gt;</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">&quot;&amp;Find&quot;</span>));
      findButton<span class="operator">-</span><span class="operator">&gt;</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">&quot;&amp;More&quot;</span>));
      moreButton<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);

</pre>
<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>
<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">&quot;&amp;Whole words&quot;</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">&quot;Search &amp;backward&quot;</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">&quot;Search se&amp;lection&quot;</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">

      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="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Vertical);
      buttonBox<span class="operator">-</span><span class="operator">&gt;</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">&gt;</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> <span class="operator">&amp;</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span><span class="operator">::</span>toggled<span class="operator">,</span> extension<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>setVisible);

      <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">&gt;</span>setMargin(<span class="number">0</span>);
      extensionLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(wholeWordsCheckBox);
      extensionLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(backwardCheckBox);
      extensionLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(searchSelectionCheckBox);
      extension<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>addWidget(label);
      topLeftLayout<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>addLayout(topLeftLayout);
      leftLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(caseCheckBox);
      leftLayout<span class="operator">-</span><span class="operator">&gt;</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>;
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>setSizeConstraint(<span class="type"><a href="qlayout.html">QLayout</a></span><span class="operator">::</span>SetFixedSize);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addLayout(leftLayout<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(buttonBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>setRowStretch(<span class="number">2</span><span class="operator">,</span> <span class="number">1</span>);

      setLayout(mainLayout);

      setWindowTitle(tr(<span class="string">&quot;Extension&quot;</span>));

</pre>
<p>Before we create the main layout, we create several child layouts for the widgets: First we align the <a href="qlabel.html">QLabel</a> and its buddy, the <a href="qlineedit.html">QLineEdit</a>, using a <a href="qhboxlayout.html">QHBoxLayout</a>. Then we vertically align 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">&gt;</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>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-dialogs-extension-finddialog-cpp.html">dialogs/extension/finddialog.cpp</a></li>
<li><a href="qtwidgets-dialogs-extension-finddialog-h.html">dialogs/extension/finddialog.h</a></li>
<li><a href="qtwidgets-dialogs-extension-main-cpp.html">dialogs/extension/main.cpp</a></li>
<li><a href="qtwidgets-dialogs-extension-extension-pro.html">dialogs/extension/extension.pro</a></li>
</ul>
</div>
<!-- @@@dialogs/extension -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    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.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>