Qt 5.10 Book Examples
FileFinder.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // FileFinder.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include "FileFinder.h"
13 
14 // ----------------------------------------------------------------------
15 FileFinder::FileFinder(QWidget* pwgt/*= 0*/) : QWidget(pwgt)
16 {
17  m_ptxtDir = new QLineEdit(QCoreApplication::applicationDirPath());
18  m_ptxtMask = new QLineEdit("*");
19  m_ptxtResult = new QTextEdit;
20 
21  QLabel* plblDir = new QLabel("&Directory");
22  QLabel* plblMask = new QLabel("&Mask");
23  QPushButton* pcmdDir = new QPushButton(QPixmap(":/fileopen.png"), "");
24  QPushButton* pcmdFind = new QPushButton("&Find");
25 
26  connect(pcmdDir, SIGNAL(clicked()), SLOT(slotBrowse()));
27  connect(pcmdFind, SIGNAL(clicked()), SLOT(slotFind()));
28 
29  plblDir->setBuddy(m_ptxtDir);
30  plblMask->setBuddy(m_ptxtMask);
31 
32  //Layout setup
33  QGridLayout* pgrdLayout = new QGridLayout;
34  pgrdLayout->setContentsMargins(5, 5, 5, 5);
35  pgrdLayout->setSpacing(15);
36  pgrdLayout->addWidget(plblDir, 0, 0);
37  pgrdLayout->addWidget(plblMask, 1, 0);
38  pgrdLayout->addWidget(m_ptxtDir, 0, 1);
39  pgrdLayout->addWidget(m_ptxtMask, 1, 1);
40  pgrdLayout->addWidget(pcmdDir, 0, 2);
41  pgrdLayout->addWidget(pcmdFind, 1, 2);
42  pgrdLayout->addWidget(m_ptxtResult, 2, 0, 1, 3);
43  setLayout(pgrdLayout);
44 }
45 
46 // ----------------------------------------------------------------------
48 {
49  QString str = QFileDialog::getExistingDirectory(0,
50  "Select a Directory",
51  m_ptxtDir->text()
52  );
53 
54  if (!str.isEmpty()) {
55  m_ptxtDir->setText(str);
56  }
57 }
58 
59 // ----------------------------------------------------------------------
61 {
62  start(QDir(m_ptxtDir->text()));
63 }
64 
65 // ----------------------------------------------------------------------
66 void FileFinder::start(const QDir& dir)
67 {
68  QApplication::processEvents();
69 
70  QStringList listFiles =
71  dir.entryList(m_ptxtMask->text().split(" "), QDir::Files);
72 
73  foreach (QString file, listFiles) {
74  m_ptxtResult->append(dir.absoluteFilePath(file));
75  }
76 
77  QStringList listDir = dir.entryList(QDir::Dirs);
78  foreach (QString subdir, listDir) {
79  if (subdir == "." || subdir == "..") {
80  continue;
81  }
82  start(QDir(dir.absoluteFilePath(subdir)));
83  }
84 }
void start(const QDir &dir)
Definition: FileFinder.cpp:66
void slotBrowse()
Definition: FileFinder.cpp:47
void slotFind()
Definition: FileFinder.cpp:60
FileFinder(QWidget *pwgt=0)
Definition: FileFinder.cpp:15