Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 1544

qt-doc-4.8.5-10.fc18.noarch.rpm

<?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" />
<!-- findfiles.qdoc -->
  <title>Qt 4.8: Find Files Example</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
  <script src="scripts/jquery.js" type="text/javascript"></script>
  <script src="scripts/functions.js" type="text/javascript"></script>
  <link rel="stylesheet" type="text/css" href="style/superfish.css" />
  <link rel="stylesheet" type="text/css" href="style/narrow.css" />
  <!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->

<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>

</head>
<body class="" onload="CheckEmptyAndLoadList();">
 <div class="header" id="qtdocheader">
    <div class="content"> 
    <div id="nav-logo">
      <a href="index.html">Home</a></div>
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
    <div id="narrowsearch"></div>
    <div id="nav-topright">
      <ul>
        <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
        <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
        <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
          DOC</a></li>
        <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
      </ul>
    </div>
    <div id="shortCut">
      <ul>
        <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
        <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS        </a></li>
      </ul>
     </div>
 <ul class="sf-menu" id="narrowmenu"> 
             <li><a href="#">API Lookup</a> 
                 <ul> 
                     <li><a href="classes.html">Class index</a></li> 
           <li><a href="functions.html">Function index</a></li> 
           <li><a href="modules.html">Modules</a></li> 
           <li><a href="namespaces.html">Namespaces</a></li> 
           <li><a href="qtglobal.html">Global Declarations</a></li> 
           <li><a href="qdeclarativeelements.html">QML elements</a></li> 
             </ul> 
             </li> 
             <li><a href="#">Qt Topics</a> 
                 <ul> 
                        <li><a href="qt-basic-concepts.html">Programming with Qt</a></li>  
                        <li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>  
                        <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>  
                        <li><a href="supported-platforms.html">Supported Platforms</a></li>  
                        <li><a href="technology-apis.html">Qt and Key Technologies</a></li>  
                        <li><a href="best-practices.html">How-To's and Best Practices</a></li>  
              </ul> 
                 </li> 
                 <li><a href="#">Examples</a> 
                     <ul> 
                       <li><a href="all-examples.html">Examples</a></li> 
                       <li><a href="tutorials.html">Tutorials</a></li> 
                       <li><a href="demos.html">Demos</a></li> 
                       <li><a href="qdeclarativeexamples.html">QML Examples</a></li> 
                </ul> 
                     </li> 
                 </ul> 
    </div>
  </div>
  <div class="wrapper">
    <div class="hd">
      <span></span>
    </div>
    <div class="bd group">
      <div class="sidebar">
        <div class="searchlabel">
          Search index:</div>
        <div class="search" id="sidebarsearch">
          <form id="qtdocsearch" action="" onsubmit="return false;">
            <fieldset>
              <input type="text" name="searchstring" id="pageType" value="" />
 <div id="resultdialog"> 
 <a href="#" id="resultclose">Close</a> 
 <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> 
 <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p> 
 <ul id="resultlist" class="all"> 
 </ul> 
 </div> 
            </fieldset>
          </form>
        </div>
        <div class="box first bottombar" id="lookup">
          <h2 title="API Lookup"><span></span>
            API Lookup</h2>
          <div  id="list001" class="list">
          <ul id="ul001" >
              <li class="defaultLink"><a href="classes.html">Class index</a></li>
              <li class="defaultLink"><a href="functions.html">Function index</a></li>
              <li class="defaultLink"><a href="modules.html">Modules</a></li>
              <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
              <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
              <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
            </ul> 
          </div>
        </div>
        <div class="box bottombar" id="topics">
          <h2 title="Qt Topics"><span></span>
            Qt Topics</h2>
          <div id="list002" class="list">
            <ul id="ul002" >
               <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> 
               <li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li> 
               <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> 
               <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>  
               <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> 
               <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> 
            </ul>  
          </div>
        </div>
        <div class="box" id="examples">
          <h2 title="Examples"><span></span>
            Examples</h2>
          <div id="list003" class="list">
        <ul id="ul003">
              <li class="defaultLink"><a href="all-examples.html">Examples</a></li>
              <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
              <li class="defaultLink"><a href="demos.html">Demos</a></li>
              <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
            </ul> 
          </div>
        </div>
      </div>
      <div class="wrap">
        <div class="toolbar">
          <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>Find Files Example</li>
            </ul>
          </div>
          <div class="toolbuttons toolblock">
            <ul>
              <li id="smallA" class="t_button">A</li>
              <li id="medA" class="t_button active">A</li>
              <li id="bigA" class="t_button">A</li>
              <li id="print" class="t_button"><a href="javascript:this.print();">
                <span>Print</span></a></li>
            </ul>
        </div>
        </div>
        <div class="content mainContent">
<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>
<h1 class="title">Find Files Example</h1>
<span class="subtitle"></span>
<!-- $$$dialogs/findfiles-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="dialogs-findfiles-window-cpp.html">dialogs/findfiles/window.cpp</a></li>
<li><a href="dialogs-findfiles-window-h.html">dialogs/findfiles/window.h</a></li>
<li><a href="dialogs-findfiles-main-cpp.html">dialogs/findfiles/main.cpp</a></li>
<li><a href="dialogs-findfiles-findfiles-pro.html">dialogs/findfiles/findfiles.pro</a></li>
</ul>
<p>The Find Files example shows how to use <a href="qprogressdialog.html">QProgressDialog</a> to provide feedback on the progress of a slow operation.<p>The example also shows how to use <a href="qfiledialog.html">QFileDialog</a> to facilitate browsing, how to use <a href="qtextstream.html">QTextStream</a>'s streaming operators to read a file, and how to use <a href="qtablewidget.html">QTableWidget</a> to provide standard table display facilities for applications. In addition, files can be opened using the <a href="qdesktopservices.html">QDesktopServices</a> class.</p>
<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 appropiate) 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>Window Class Definition</h2>
<p>The <tt>Window</tt> 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> openFileOfItem(<span class="type">int</span> row<span class="operator">,</span> <span class="type">int</span> column);

 <span class="keyword">private</span>:
     <span class="type"><a href="qstringlist.html">QStringList</a></span> findFiles(<span class="keyword">const</span> <span class="type"><a href="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="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="qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>files);
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>createButton(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>text<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>member);
     <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="qstring.html">QString</a></span> <span class="operator">&amp;</span>text <span class="operator">=</span> <span class="type"><a href="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>fileLabel;
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>textLabel;
     <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>directoryLabel;
     <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>browseButton;
     <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="qdir.html">QDir</a></span> currentDir;
 };</pre>
<p>We need two private slots: The <tt>browse()</tt> slot is called whenever the user wants to browse for a directory to search in, and the <tt>find()</tt> 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 <tt>findFiles()</tt> function to search for files matching the user's specifications, we call the <tt>showFiles()</tt> function to display the results, and we use <tt>createButton()</tt>, <tt>createComboBox()</tt> and <tt>createFilesTable()</tt> when we are constructing the widget.</p>
<a name="window-class-implementation"></a>
<h2>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)
 {
     browseButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;&amp;Browse...&quot;</span>)<span class="operator">,</span> SLOT(browse()));
     findButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;&amp;Find&quot;</span>)<span class="operator">,</span> SLOT(find()));

     fileComboBox <span class="operator">=</span> createComboBox(tr(<span class="string">&quot;*&quot;</span>));
     textComboBox <span class="operator">=</span> createComboBox();
     directoryComboBox <span class="operator">=</span> createComboBox(<span class="type"><a href="qdir.html">QDir</a></span><span class="operator">::</span>currentPath());

     fileLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Named:&quot;</span>));
     textLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Containing text:&quot;</span>));
     directoryLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;In directory:&quot;</span>));
     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 <tt>createButton()</tt> function. Then we create the comboboxes associated with the search specifications, using the private <tt>createComboBox()</tt> function. We also create the application's labels before we use the private <tt>createFilesTable()</tt> 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>;
     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>SetNoConstraint);
     mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(fileLabel<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(textLabel<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(directoryLabel<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>);
     setLayout(mainLayout);

     setWindowTitle(tr(<span class="string">&quot;Find Files&quot;</span>));
 <span class="preprocessor">#if !defined(Q_OS_SYMBIAN) &amp;&amp; !defined(Q_WS_MAEMO_5) &amp;&amp; !defined(Q_WS_SIMULATOR)</span>
     resize(<span class="number">700</span><span class="operator">,</span> <span class="number">300</span>);
 <span class="preprocessor">#endif</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 <tt>Find</tt> and <tt>Quit</tt> buttons and a stretchable space in a separate <a href="qhboxlayout.html">QHBoxLayout</a> first, to make the buttons appear in the <tt>Window</tt> 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="qstring.html">QString</a></span> directory <span class="operator">=</span> <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="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 <tt>browse()</tt> 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="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="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="qstring.html">QString</a></span> path <span class="operator">=</span> directoryComboBox<span class="operator">-</span><span class="operator">&gt;</span>currentText();</pre>
<p>The <tt>find()</tt> 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="qdir.html">QDir</a></span>(path);
     <span class="type"><a href="qstringlist.html">QStringList</a></span> files;
     <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
         fileName <span class="operator">=</span> <span class="string">&quot;*&quot;</span>;
     files <span class="operator">=</span> currentDir<span class="operator">.</span>entryList(<span class="type"><a href="qstringlist.html">QStringList</a></span>(fileName)<span class="operator">,</span>
                                  <span class="type"><a href="qdir.html">QDir</a></span><span class="operator">::</span>Files <span class="operator">|</span> <span class="type"><a href="qdir.html">QDir</a></span><span class="operator">::</span>NoSymLinks);

     <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="qdir.html">QDir</a>; the <a href="qdir.html">QDir</a> class provides access to directory structures and their contents. We create a list of the files (contained in the newly created <a href="qdir.html">QDir</a>) that match the specified file name. If the file name is empty the list will contain all the files in the directory.</p>
<p>Then we search through all the files in the list, using the private <tt>findFiles()</tt> function, eliminating the ones that don't contain the specified text. And finally, we display the results using the private <tt>showFiles()</tt> 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="qstringlist.html">QStringList</a></span> Window<span class="operator">::</span>findFiles(<span class="keyword">const</span> <span class="type"><a href="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="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 <tt>findFiles()</tt> 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="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 %2...&quot;</span>)
                                     <span class="operator">.</span>arg(i)<span class="operator">.</span>arg(files<span class="operator">.</span>size()));
         qApp<span class="operator">-</span><span class="operator">&gt;</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="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="qcoreapplication.html">QCoreApplication</a>. The <a href="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="qeventloop.html#ProcessEventsFlag-enum">QEventLoop::AllEvents</a>.</p>
<pre class="cpp">         <span class="type"><a href="qfile.html">QFile</a></span> file(currentDir<span class="operator">.</span>absoluteFilePath(files<span class="operator">[</span>i<span class="operator">]</span>));

         <span class="keyword">if</span> (file<span class="operator">.</span>open(<span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly)) {
             <span class="type"><a href="qstring.html">QString</a></span> line;
             <span class="type"><a href="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)) {
                     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 create a <a href="qfile.html">QFile</a> using the <a href="qdir.html#absoluteFilePath">QDir::absoluteFilePath</a>() function which returns the absolute path name of a file in the directory. We open the file in read-only mode, and read one line at a time using <a href="qtextstream.html">QTextStream</a>.</p>
<p>The <a href="qtextstream.html">QTextStream</a> class provides a convenient interface for reading and writing text. Using <a href="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="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="type"><a href="qfile.html">QFile</a></span> file(currentDir<span class="operator">.</span>absoluteFilePath(files<span class="operator">[</span>i<span class="operator">]</span>));
         <span class="type"><a href="qtglobal.html#qint64-typedef">qint64</a></span> size <span class="operator">=</span> <span class="type"><a href="qfileinfo.html">QFileInfo</a></span>(file)<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>(files<span class="operator">[</span>i<span class="operator">]</span>);
         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="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>setTextAlignment(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignRight <span class="operator">|</span> <span class="type"><a href="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="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;%1 file(s) found&quot;</span>)<span class="operator">.</span>arg(files<span class="operator">.</span>size()) <span class="operator">+</span>
 <span class="preprocessor">#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)</span>
                              (<span class="string">&quot; (Select file to open it)&quot;</span>));
 <span class="preprocessor">#else</span>
                              (<span class="string">&quot; (Double click on a file to open it)&quot;</span>));
 <span class="preprocessor">#endif</span>
     filesFoundLabel<span class="operator">-</span><span class="operator">&gt;</span>setWordWrap(<span class="keyword">true</span>);
 }</pre>
<p>Both the <tt>findFiles()</tt> and <tt>showFiles()</tt> functions are called from the <tt>find()</tt> slot. In the <tt>showFiles()</tt> function we run through the provided list of file names, adding each file name to the first column in the table widget and retrieving the file's size using <a href="qfile.html">QFile</a> and <a href="qfileinfo.html">QFileInfo</a> for the second column.</p>
<p>We also update the total number of files found.</p>
<pre class="cpp"> <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>Window<span class="operator">::</span>createButton(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>text<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>member)
 {
     <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(text);
     connect(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> member);
     <span class="keyword">return</span> button;
 }</pre>
<p>The private <tt>createButton()</tt> function is called from the constructor. We create a <a href="qpushbutton.html">QPushButton</a> with the provided text, connect it to the provided slot, and return a pointer to the button.</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="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="preprocessor">#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)</span>
     comboBox<span class="operator">-</span><span class="operator">&gt;</span>setMinimumContentsLength(<span class="number">3</span>);
 <span class="preprocessor">#endif</span>
     <span class="keyword">return</span> comboBox;
 }</pre>
<p>The private <tt>createComboBox()</tt> 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="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>setResizeMode(<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>);

     connect(filesTable<span class="operator">,</span> SIGNAL(cellActivated(<span class="type">int</span><span class="operator">,</span><span class="type">int</span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(openFileOfItem(<span class="type">int</span><span class="operator">,</span><span class="type">int</span>)));
 }</pre>
<p>The private <tt>createFilesTable()</tt> 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 <a href="qheaderview.html#setResizeMode">QHeaderView::setResizeMode</a>() 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="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>);

     <span class="type"><a href="qdesktopservices.html">QDesktopServices</a></span><span class="operator">::</span>openUrl(<span class="type"><a href="qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(currentDir<span class="operator">.</span>absoluteFilePath(item<span class="operator">-</span><span class="operator">&gt;</span>text())));
 }</pre>
<p>The <tt>openFileOfItem()</tt> slot is invoked when the user double clicks on a cell in the table. The <a href="qdesktopservices.html#openUrl">QDesktopServices::openUrl</a>() knows how to open a file given the file name.</p>
</div>
<!-- @@@dialogs/findfiles -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc 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>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      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>

  <script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>