Sophie

Sophie

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

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" />
<!-- findfiles.qdoc -->
  <title>Find Files 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 >Find Files 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="#window-class-definition">Window Class Definition</a></li>
<li class="level1"><a href="#window-class-implementation">Window Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Find Files Example</h1>
<span class="subtitle"></span>
<!-- $$$dialogs/findfiles-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/findfiles-example.png" alt="Screenshot of the Find Files example" /></p><p>With the Find Files application the user can search for files in a specified directory, matching a specified file name (using wild cards if appropriate) and containing a specified text.</p>
<p>The user is provided with a <b>Browse</b> option, and the result of the search is displayed in a table with the names of the files found and their sizes. In addition the application provides a total count of the files found.</p>
<a name="window-class-definition"></a>
<h2 id="window-class-definition">Window Class Definition</h2>
<p>The <code>Window</code> class inherits <a href="qwidget.html">QWidget</a>, and is the main application widget. It shows the search options, and displays the search results.</p>
<pre class="cpp">

  <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      Window(<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="keyword">slots</span>:
      <span class="type">void</span> browse();
      <span class="type">void</span> find();
      <span class="type">void</span> animateFindClick();
      <span class="type">void</span> openFileOfItem(<span class="type">int</span> row<span class="operator">,</span> <span class="type">int</span> column);
      <span class="type">void</span> contextMenu(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> <span class="operator">&amp;</span>pos);

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> findFiles(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>files<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text);
      <span class="type">void</span> showFiles(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>files);
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>createComboBox(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text <span class="operator">=</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>());
      <span class="type">void</span> createFilesTable();

      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>fileComboBox;
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>textComboBox;
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>directoryComboBox;
      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>filesFoundLabel;
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>findButton;
      <span class="type"><a href="qtablewidget.html">QTableWidget</a></span> <span class="operator">*</span>filesTable;

      <span class="type"><a href="../qtcore/qdir.html">QDir</a></span> currentDir;
  };

</pre>
<p>We need two private slots: The <code>browse()</code> slot is called whenever the user wants to browse for a directory to search in, and the <code>find()</code> slot is called whenever the user requests a search to be performed by pressing the <b>Find</b> button.</p>
<p>In addition we declare several private functions: We use the <code>findFiles()</code> function to search for files matching the user's specifications, we call the <code>showFiles()</code> function to display the results, and we use <code>createButton()</code>, <code>createComboBox()</code> and <code>createFilesTable()</code> when we are constructing the widget.</p>
<a name="window-class-implementation"></a>
<h2 id="window-class-implementation">Window Class Implementation</h2>
<p>In the constructor we first create the application's widgets.</p>
<pre class="cpp">

  Window<span class="operator">::</span>Window(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
  {
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>browseButton <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;Browse...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(browseButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>browse);
      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>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(findButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>find);

      fileComboBox <span class="operator">=</span> createComboBox(tr(<span class="string">&quot;*&quot;</span>));
      connect(fileComboBox<span class="operator">-</span><span class="operator">&gt;</span>lineEdit()<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">::</span>returnPressed<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>animateFindClick);
      textComboBox <span class="operator">=</span> createComboBox();
      connect(textComboBox<span class="operator">-</span><span class="operator">&gt;</span>lineEdit()<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">::</span>returnPressed<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>animateFindClick);
      directoryComboBox <span class="operator">=</span> createComboBox(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>currentPath()));
      connect(directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>lineEdit()<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">::</span>returnPressed<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>animateFindClick);

      filesFoundLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>;

      createFilesTable();

</pre>
<p>We create the application's buttons using the private <code>createButton()</code> function. Then we create the comboboxes associated with the search specifications, using the private <code>createComboBox()</code> function. We also create the application's labels before we use the private <code>createFilesTable()</code> function to create the table displaying the search results.</p>
<pre class="cpp">

      <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="keyword">this</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Named:&quot;</span>))<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(fileComboBox<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">1</span><span class="operator">,</span> <span class="number">2</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Containing text:&quot;</span>))<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(textComboBox<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</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>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;In directory:&quot;</span>))<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(directoryComboBox<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(browseButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">2</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(filesTable<span class="operator">,</span> <span class="number">3</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">3</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(filesFoundLabel<span class="operator">,</span> <span class="number">4</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>addWidget(findButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">2</span>);

      setWindowTitle(tr(<span class="string">&quot;Find Files&quot;</span>));
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> screenGeometry <span class="operator">=</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>desktop()<span class="operator">-</span><span class="operator">&gt;</span>screenGeometry(<span class="keyword">this</span>);
      resize(screenGeometry<span class="operator">.</span>width() <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> screenGeometry<span class="operator">.</span>height() <span class="operator">/</span> <span class="number">3</span>);
  }

</pre>
<p>Then we add all the widgets to a main layout using <a href="qgridlayout.html">QGridLayout</a>. We have, however, put the <code>Find</code> and <code>Quit</code> buttons and a stretchable space in a separate <a href="qhboxlayout.html">QHBoxLayout</a> first, to make the buttons appear in the <code>Window</code> widget's bottom right corner.</p>
<pre class="cpp">

  <span class="type">void</span> Window<span class="operator">::</span>browse()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> directory <span class="operator">=</span>
          <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(<span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getExistingDirectory(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Find Files&quot;</span>)<span class="operator">,</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>currentPath()));

      <span class="keyword">if</span> (<span class="operator">!</span>directory<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>findText(directory) <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>)
              directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>addItem(directory);
          directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>findText(directory));
      }
  }

</pre>
<p>The <code>browse()</code> slot presents a file dialog to the user, using the <a href="qfiledialog.html">QFileDialog</a> class. <a href="qfiledialog.html">QFileDialog</a> enables a user to traverse the file system in order to select one or many files or a directory. The easiest way to create a <a href="qfiledialog.html">QFileDialog</a> is to use the convenience static functions.</p>
<p>Here we use the static <a href="qfiledialog.html#getExistingDirectory">QFileDialog::getExistingDirectory</a>() function which returns an existing directory selected by the user. Then we display the directory in the directory combobox using the <a href="qcombobox.html#addItem">QComboBox::addItem</a>() function, and updates the current index.</p>
<p><a href="qcombobox.html#addItem">QComboBox::addItem</a>() adds an item to the combobox with the given text (if it is not already present in the list), and containing the specified userData. The item is appended to the list of existing items.</p>
<pre class="cpp">

  <span class="type">void</span> Window<span class="operator">::</span>find()
  {
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>setRowCount(<span class="number">0</span>);

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> fileComboBox<span class="operator">-</span><span class="operator">&gt;</span>currentText();
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> textComboBox<span class="operator">-</span><span class="operator">&gt;</span>currentText();
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> path <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>cleanPath(directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>currentText());

</pre>
<p>The <code>find()</code> slot is called whenever the user requests a new search by pressing the <b>Find</b> button.</p>
<p>First we eliminate any previous search results by setting the table widgets row count to zero. Then we retrieve the specified file name, text and directory path from the respective comboboxes.</p>
<pre class="cpp">

      currentDir <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span>(path);
      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> files;
      findRecursion(path<span class="operator">,</span> fileName<span class="operator">.</span>isEmpty() <span class="operator">?</span> <span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;*&quot;</span>) : fileName<span class="operator">,</span> <span class="operator">&amp;</span>files);
      <span class="keyword">if</span> (<span class="operator">!</span>text<span class="operator">.</span>isEmpty())
          files <span class="operator">=</span> findFiles(files<span class="operator">,</span> text);
      showFiles(files);
  }

</pre>
<p>We use the directory's path to create a <a href="../qtcore/qdir.html">QDir</a>; the <a href="../qtcore/qdir.html">QDir</a> class provides access to directory structures and their contents.</p>
<pre class="cpp">

  <span class="keyword">static</span> <span class="type">void</span> findRecursion(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>path<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>pattern<span class="operator">,</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> <span class="operator">*</span>result)
  {
      <span class="type"><a href="../qtcore/qdir.html">QDir</a></span> currentDir(path);
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> prefix <span class="operator">=</span> path <span class="operator">+</span> QLatin1Char(<span class="char">'/'</span>);
      foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>match<span class="operator">,</span> currentDir<span class="operator">.</span>entryList(<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>(pattern)<span class="operator">,</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>Files <span class="operator">|</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>NoSymLinks))
          result<span class="operator">-</span><span class="operator">&gt;</span>append(prefix <span class="operator">+</span> match);
      foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>dir<span class="operator">,</span> currentDir<span class="operator">.</span>entryList(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>Dirs <span class="operator">|</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>NoSymLinks <span class="operator">|</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>NoDotAndDotDot))
          findRecursion(prefix <span class="operator">+</span> dir<span class="operator">,</span> pattern<span class="operator">,</span> result);
  }

</pre>
<p>We recursively create a list of the files (contained in the newl created <a href="../qtcore/qdir.html">QDir</a>) that match the specified file name.</p>
<p>Then we search through all the files in the list, using the private <code>findFiles()</code> function, eliminating the ones that don't contain the specified text. And finally, we display the results using the private <code>showFiles()</code> function.</p>
<p>If the user didn't specify any text, there is no reason to search through the files, and we display the results immediately.</p>
<p class="centerAlign"><img src="images/findfiles_progress_dialog.png" alt="Screenshot of the Progress Dialog" /></p><pre class="cpp">

  <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> Window<span class="operator">::</span>findFiles(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>files<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text)
  {
      <span class="type"><a href="qprogressdialog.html">QProgressDialog</a></span> progressDialog(<span class="keyword">this</span>);
      progressDialog<span class="operator">.</span>setCancelButtonText(tr(<span class="string">&quot;&amp;Cancel&quot;</span>));
      progressDialog<span class="operator">.</span>setRange(<span class="number">0</span><span class="operator">,</span> files<span class="operator">.</span>size());
      progressDialog<span class="operator">.</span>setWindowTitle(tr(<span class="string">&quot;Find Files&quot;</span>));

</pre>
<p>In the private <code>findFiles()</code> function we search through a list of files, looking for the ones that contain a specified text. This can be a very slow operation depending on the number of files as well as their sizes. In case there are a large number of files, or there exists some large files on the list, we provide a <a href="qprogressdialog.html">QProgressDialog</a>.</p>
<p>The <a href="qprogressdialog.html">QProgressDialog</a> class provides feedback on the progress of a slow operation. It is used to give the user an indication of how long an operation is going to take, and to demonstrate that the application has not frozen. It can also give the user an opportunity to abort the operation.</p>
<pre class="cpp">

      <span class="type"><a href="../qtcore/qmimedatabase.html">QMimeDatabase</a></span> mimeDatabase;
      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> foundFiles;

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> files<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
          progressDialog<span class="operator">.</span>setValue(i);
          progressDialog<span class="operator">.</span>setLabelText(tr(<span class="string">&quot;Searching file number %1 of %n...&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> files<span class="operator">.</span>size())<span class="operator">.</span>arg(i));
          <span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>processEvents();

</pre>
<p>We run through the files, one at a time, and for each file we update the <a href="qprogressdialog.html">QProgressDialog</a> value. This property holds the current amount of progress made. We also update the progress dialog's label.</p>
<p>Then we call the <a href="../qtcore/qcoreapplication.html#processEvents">QCoreApplication::processEvents</a>() function using the <a href="qapplication.html">QApplication</a> object. In this way we interleave the display of the progress made with the process of searching through the files so the application doesn't appear to be frozen.</p>
<p>The <a href="qapplication.html">QApplication</a> class manages the GUI application's control flow and main settings. It contains the main event loop, where all events from the window system and other sources are processed and dispatched. <a href="qapplication.html">QApplication</a> inherits <a href="../qtcore/qcoreapplication.html">QCoreApplication</a>. The <a href="../qtcore/qcoreapplication.html#processEvents">QCoreApplication::processEvents</a>() function processes all pending events according to the specified QEventLoop::ProcessEventFlags until there are no more events to process. The default flags are <a href="../qtcore/qeventloop.html#ProcessEventsFlag-enum">QEventLoop::AllEvents</a>.</p>
<pre class="cpp">

          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> files<span class="operator">.</span>at(i);
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmimetype.html">QMimeType</a></span> mimeType <span class="operator">=</span> mimeDatabase<span class="operator">.</span>mimeTypeForFile(fileName);
          <span class="keyword">if</span> (mimeType<span class="operator">.</span>isValid() <span class="operator">&amp;</span><span class="operator">&amp;</span> <span class="operator">!</span>mimeType<span class="operator">.</span>inherits(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;text/plain&quot;</span>))) {
              <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Not searching binary file &quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName);
              <span class="keyword">continue</span>;
          }
          <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(fileName);
          <span class="keyword">if</span> (file<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly)) {
              <span class="type"><a href="../qtcore/qstring.html">QString</a></span> line;
              <span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> in(<span class="operator">&amp;</span>file);
              <span class="keyword">while</span> (<span class="operator">!</span>in<span class="operator">.</span>atEnd()) {
                  <span class="keyword">if</span> (progressDialog<span class="operator">.</span>wasCanceled())
                      <span class="keyword">break</span>;
                  line <span class="operator">=</span> in<span class="operator">.</span>readLine();
                  <span class="keyword">if</span> (line<span class="operator">.</span>contains(text<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>CaseInsensitive)) {
                      foundFiles <span class="operator">&lt;</span><span class="operator">&lt;</span> files<span class="operator">[</span>i<span class="operator">]</span>;
                      <span class="keyword">break</span>;
                  }
              }
          }
      }
      <span class="keyword">return</span> foundFiles;
  }

</pre>
<p>After updating the <a href="qprogressdialog.html">QProgressDialog</a>, we open the file in read-only mode, and read one line at a time using <a href="../qtcore/qtextstream.html">QTextStream</a>.</p>
<p>The <a href="../qtcore/qtextstream.html">QTextStream</a> class provides a convenient interface for reading and writing text. Using <a href="../qtcore/qtextstream.html">QTextStream</a>'s streaming operators, you can conveniently read and write words, lines and numbers.</p>
<p>For each line we read we check if the <a href="qprogressdialog.html">QProgressDialog</a> has been canceled. If it has, we abort the operation, otherwise we check if the line contains the specified text. When we find the text within one of the files, we add the file's name to a list of found files that contain the specified text, and start searching a new file.</p>
<p>Finally, we return the list of the files found.</p>
<pre class="cpp">

  <span class="type">void</span> Window<span class="operator">::</span>showFiles(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>files)
  {
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> files<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName <span class="operator">=</span> files<span class="operator">.</span>at(i);
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> toolTip <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName);
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> relativePath <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(currentDir<span class="operator">.</span>relativeFilePath(fileName));
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#qint64-typedef">qint64</a></span> size <span class="operator">=</span> <span class="type"><a href="../qtcore/qfileinfo.html">QFileInfo</a></span>(fileName)<span class="operator">.</span>size();
          <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>fileNameItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(relativePath);
          fileNameItem<span class="operator">-</span><span class="operator">&gt;</span>setData(absoluteFileNameRole<span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>(fileName));
          fileNameItem<span class="operator">-</span><span class="operator">&gt;</span>setToolTip(toolTip);
          fileNameItem<span class="operator">-</span><span class="operator">&gt;</span>setFlags(fileNameItem<span class="operator">-</span><span class="operator">&gt;</span>flags() <span class="operator">^</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable);
          <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>sizeItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(tr(<span class="string">&quot;%1 KB&quot;</span>)
                                               <span class="operator">.</span>arg(<span class="type">int</span>((size <span class="operator">+</span> <span class="number">1023</span>) <span class="operator">/</span> <span class="number">1024</span>)));
          sizeItem<span class="operator">-</span><span class="operator">&gt;</span>setData(absoluteFileNameRole<span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>(fileName));
          sizeItem<span class="operator">-</span><span class="operator">&gt;</span>setToolTip(toolTip);
          sizeItem<span class="operator">-</span><span class="operator">&gt;</span>setTextAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignRight <span class="operator">|</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignVCenter);
          sizeItem<span class="operator">-</span><span class="operator">&gt;</span>setFlags(sizeItem<span class="operator">-</span><span class="operator">&gt;</span>flags() <span class="operator">^</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ItemIsEditable);

          <span class="type">int</span> row <span class="operator">=</span> filesTable<span class="operator">-</span><span class="operator">&gt;</span>rowCount();
          filesTable<span class="operator">-</span><span class="operator">&gt;</span>insertRow(row);
          filesTable<span class="operator">-</span><span class="operator">&gt;</span>setItem(row<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> fileNameItem);
          filesTable<span class="operator">-</span><span class="operator">&gt;</span>setItem(row<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> sizeItem);
      }
      filesFoundLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;%n file(s) found (Double click on a file to open it)&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> files<span class="operator">.</span>size()));
      filesFoundLabel<span class="operator">-</span><span class="operator">&gt;</span>setWordWrap(<span class="keyword">true</span>);
  }

</pre>
<p>Both the <code>findFiles()</code> and <code>showFiles()</code> functions are called from the <code>find()</code> slot. In the <code>showFiles()</code> function we run through the provided list of file names, adding each relative file name to the first column in the table widget and retrieving the file's size using <a href="../qtcore/qfileinfo.html">QFileInfo</a> for the second column. For later use, we set the absolute path as a data on the <a href="qtablewidget.html">QTableWidget</a> using the the role absoluteFileNameRole defined to be <a href="../qtcore/qt.html#ItemDataRole-enum">Qt::UserRole</a> + 1.</p>
<pre class="cpp">

  <span class="keyword">enum</span> { absoluteFileNameRole <span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>UserRole <span class="operator">+</span> <span class="number">1</span> };

</pre>
<p>This allows for retrieving the name of an item using a convenience function:</p>
<pre class="cpp">

  <span class="keyword">static</span> <span class="keyword">inline</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileNameOfItem(<span class="keyword">const</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>item)
  {
      <span class="keyword">return</span> item<span class="operator">-</span><span class="operator">&gt;</span>data(absoluteFileNameRole)<span class="operator">.</span>toString();
  }

</pre>
<p>We also update the total number of files found.</p>
<pre class="cpp">

  <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>Window<span class="operator">::</span>createComboBox(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text)
  {
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>comboBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>;
      comboBox<span class="operator">-</span><span class="operator">&gt;</span>setEditable(<span class="keyword">true</span>);
      comboBox<span class="operator">-</span><span class="operator">&gt;</span>addItem(text);
      comboBox<span class="operator">-</span><span class="operator">&gt;</span>setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Preferred);
      <span class="keyword">return</span> comboBox;
  }

</pre>
<p>The private <code>createComboBox()</code> function is also called from the contructor. We create a <a href="qcombobox.html">QComboBox</a> with the given text, and make it editable.</p>
<p>When the user enters a new string in an editable combobox, the widget may or may not insert it, and it can insert it in several locations, depending on the <a href="qcombobox.html#InsertPolicy-enum">QComboBox::InsertPolicy</a>. The default policy is is <a href="qcombobox.html#InsertPolicy-enum">QComboBox::InsertAtBottom</a>.</p>
<p>Then we add the provided text to the combobox, and specify the widget's size policies, before we return a pointer to the combobox.</p>
<pre class="cpp">

  <span class="type">void</span> Window<span class="operator">::</span>createFilesTable()
  {
      filesTable <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidget.html">QTableWidget</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">2</span>);
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>setSelectionBehavior(<span class="type"><a href="qabstractitemview.html">QAbstractItemView</a></span><span class="operator">::</span>SelectRows);

      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> labels;
      labels <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;Filename&quot;</span>) <span class="operator">&lt;</span><span class="operator">&lt;</span> tr(<span class="string">&quot;Size&quot;</span>);
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>setHorizontalHeaderLabels(labels);
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>horizontalHeader()<span class="operator">-</span><span class="operator">&gt;</span>setSectionResizeMode(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="qheaderview.html">QHeaderView</a></span><span class="operator">::</span>Stretch);
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>verticalHeader()<span class="operator">-</span><span class="operator">&gt;</span>hide();
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>setShowGrid(<span class="keyword">false</span>);
      filesTable<span class="operator">-</span><span class="operator">&gt;</span>setContextMenuPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>CustomContextMenu);
      connect(filesTable<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qtablewidget.html">QTableWidget</a></span><span class="operator">::</span>customContextMenuRequested<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>contextMenu);
      connect(filesTable<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qtablewidget.html">QTableWidget</a></span><span class="operator">::</span>cellActivated<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>openFileOfItem);
  }

</pre>
<p>The private <code>createFilesTable()</code> function is called from the constructor. In this function we create the <a href="qtablewidget.html">QTableWidget</a> that will display the search results. We set its horizontal headers and their resize mode.</p>
<p><a href="qtablewidget.html">QTableWidget</a> inherits <a href="qtableview.html">QTableView</a> which provides a default model/view implementation of a table view. The <a href="qtableview.html#horizontalHeader">QTableView::horizontalHeader</a>() function returns the table view's horizontal header as a <a href="qheaderview.html">QHeaderView</a>. The <a href="qheaderview.html">QHeaderView</a> class provides a header row or header column for item views, and the QHeaderView::setResizeMode() function sets the constraints on how the section in the header can be resized.</p>
<p>Finally, we hide the <a href="qtablewidget.html">QTableWidget</a>'s vertical headers using the <a href="qwidget.html#hide">QWidget::hide</a>() function, and remove the default grid drawn for the table using the <a href="qtableview.html#showGrid-prop">QTableView::setShowGrid</a>() function.</p>
<pre class="cpp">

  <span class="type">void</span> Window<span class="operator">::</span>openFileOfItem(<span class="type">int</span> row<span class="operator">,</span> <span class="type">int</span> <span class="comment">/* column */</span>)
  {
      <span class="keyword">const</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> filesTable<span class="operator">-</span><span class="operator">&gt;</span>item(row<span class="operator">,</span> <span class="number">0</span>);
      openFile(fileNameOfItem(item));
  }

  <span class="keyword">static</span> <span class="keyword">inline</span> <span class="type">void</span> openFile(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>fileName)
  {
      <span class="type"><a href="../qtgui/qdesktopservices.html">QDesktopServices</a></span><span class="operator">::</span>openUrl(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(fileName));
  }

</pre>
<p>The <code>openFileOfItem()</code> slot is invoked when the user double clicks on a cell in the table. The <a href="../qtgui/qdesktopservices.html#openUrl">QDesktopServices::openUrl</a>() knows how to open a file given the file name.</p>
<pre class="cpp">

      filesTable<span class="operator">-</span><span class="operator">&gt;</span>setContextMenuPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>CustomContextMenu);
      connect(filesTable<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qtablewidget.html">QTableWidget</a></span><span class="operator">::</span>customContextMenuRequested<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>contextMenu);
      connect(filesTable<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qtablewidget.html">QTableWidget</a></span><span class="operator">::</span>cellActivated<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Window<span class="operator">::</span>openFileOfItem);
  <span class="type">void</span> Window<span class="operator">::</span>contextMenu(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> <span class="operator">&amp;</span>pos)
  {
      <span class="keyword">const</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> filesTable<span class="operator">-</span><span class="operator">&gt;</span>itemAt(pos);
      <span class="keyword">if</span> (<span class="operator">!</span>item)
          <span class="keyword">return</span>;
      <span class="type"><a href="qmenu.html">QMenu</a></span> menu;
  <span class="preprocessor">#ifndef QT_NO_CLIPBOARD</span>
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>copyAction <span class="operator">=</span> menu<span class="operator">.</span>addAction(<span class="string">&quot;Copy Name&quot;</span>);
  <span class="preprocessor">#endif</span>
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openAction <span class="operator">=</span> menu<span class="operator">.</span>addAction(<span class="string">&quot;Open&quot;</span>);
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action <span class="operator">=</span> menu<span class="operator">.</span>exec(filesTable<span class="operator">-</span><span class="operator">&gt;</span>mapToGlobal(pos));
      <span class="keyword">if</span> (<span class="operator">!</span>action)
          <span class="keyword">return</span>;
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> fileNameOfItem(item);
      <span class="keyword">if</span> (action <span class="operator">=</span><span class="operator">=</span> openAction)
          openFile(fileName);
  <span class="preprocessor">#ifndef QT_NO_CLIPBOARD</span>
      <span class="keyword">else</span> <span class="keyword">if</span> (action <span class="operator">=</span><span class="operator">=</span> copyAction)
          <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>clipboard()<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName));
  <span class="preprocessor">#endif</span>
  }

</pre>
<p>We set the context menu policy to of the table view to <a href="../qtcore/qt.html#ContextMenuPolicy-enum">Qt::CustomContextMenu</a> and connect a slot contextMenu() to its signal customContextMenuRequested(). We retrieve the absolute file name from the data of the <a href="qtablewidgetitem.html">QTableWidgetItem</a> and populate the context menu with actions offering to copy the file name and to open the file.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-dialogs-findfiles-window-cpp.html">dialogs/findfiles/window.cpp</a></li>
<li><a href="qtwidgets-dialogs-findfiles-window-h.html">dialogs/findfiles/window.h</a></li>
<li><a href="qtwidgets-dialogs-findfiles-main-cpp.html">dialogs/findfiles/main.cpp</a></li>
<li><a href="qtwidgets-dialogs-findfiles-findfiles-pro.html">dialogs/findfiles/findfiles.pro</a></li>
</ul>
</div>
<!-- @@@dialogs/findfiles -->
        </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>