Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 4283

qtbase5-doc-5.12.6-2.mga7.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.12.6</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.12</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"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></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-brief -->
<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>
<!-- @@@dialogs/extension -->
<!-- $$$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 lets the user add search parameters in a dialog and launch a simple or advanced search.</p>
<p>The simple search has two options: <b>Match case</b> and <b>Search from start</b>. The advanced search offers search for <b>Whole words</b>, <b>Search backward</b>, and <b>Search selection</b>. The application starts with simple search as the default. Click the <b>More</b> button to show the advanced search options:</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>. <a href="qdialog.html">QDialog</a> is the base class for 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 the constructor, there are several child widgets:</p>
<ul>
<li>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.</li>
<li>Several <a href="qcheckbox.html">QCheckBox</a>es to facilitate the search options.</li>
<li>Three <a href="qpushbutton.html">QPushButton</a>s:<ul>
<li>the <b>Find</b> button to start a search</li>
<li>the <b>More</b> button to enable an advanced search</li>
<li>a <a href="qwidget.html">QWidget</a> representing the application's extension part</li>
</ul>
</li>
</ul>
<a name="finddialog-class-implementation"></a>
<h2 id="finddialog-class-implementation">FindDialog Class Implementation</h2>
<p>Create the standard child widgets for the simple search in the constructor: the <a href="qlineedit.html">QLineEdit</a> with the associated <a href="qlabel.html">QLabel</a>, two {<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>This snippet illustrates how you can define a shortcut key for a widget. A shortcut should be defined by putting the ampersand character (<code>&amp;</code>) in front of the letter that should become the shortcut. For example, for <b>Find what</b>, pressing <b>Alt</b> and <b>w</b> transfers focus to the <a href="qlineedit.html">QLineEdit</a> widget. Shortcuts can also be used for checking on or off a checkmark. For example, pressing <b>Alt</b> and <b>c</b> puts the check mark on <b>Match Case</b> if it was unchecked and vice versa. It is the <a href="qlabel.html#setBuddy">QLabel::setBuddy</a>() method that links a widget to the shortcut character if it has been defined.</p>
<p>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>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, 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 the <b>More</b> button is checkable, the connection makes sure that the extension widget is shown depending on the state of the <b>More</b> button.</p>
<p>Create checkboxes associated with the advanced search options in a layout installed 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 creating the main layout, create several child layouts for the widgets. First 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 align the <a href="qlabel.html">QLabel</a> and the <a href="qlineedit.html">QLineEdit</a> vertically with the checkboxes associated with the simple search, using a <a href="qvboxlayout.html">QVBoxLayout</a>. Create also a <a href="qvboxlayout.html">QVBoxLayout</a> for the buttons. Finally, 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>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-extension-pro.html">dialogs/extension/extension.pro</a></li>
<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>
</ul>
</div>
<!-- @@@dialogs/extension -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 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>