Find Files Example▲
The Find Files application allows the user to search for files in a specified directory, matching a given file name or wildcard, and containing a specified string (if filled in). The search result is displayed in a table containing the names of the files and their sizes. The application also shows the number of files found.
The Find Files example illustrates the use of several classes:
QProgressDialog |
Provide feedback on the progress of a search operation |
QFileDialog |
Browse through a file list |
QTextStream |
Use stream operators to read a file |
QTableWidget |
Browse through the search results in a table |
QDesktopServices |
Open files in the result list in a suitable application |
Window Class Definition▲
The Window class inherits QWidget, and is the main application widget. It shows the search options and displays the search results.
class
Window : public
QWidget
{
Q_OBJECT
public
:
Window(QWidget *
parent =
0
);
private
slots:
void
browse();
void
find();
void
animateFindClick();
void
openFileOfItem(int
row, int
column);
void
contextMenu(const
QPoint &
amp;pos);
private
:
QStringList findFiles(const
QStringList &
amp;files, const
QString &
amp;text);
void
showFiles(const
QStringList &
amp;paths);
QComboBox *
createComboBox(const
QString &
amp;text =
QString());
void
createFilesTable();
QComboBox *
fileComboBox;
QComboBox *
textComboBox;
QComboBox *
directoryComboBox;
QLabel *
filesFoundLabel;
QPushButton *
findButton;
QTableWidget *
filesTable;
QDir currentDir;
}
;
The application has two private slots:
The browse() slot |
Called whenever the user wants to browse for a directory to search in |
The find() slot |
Called whenever the user launches a search with the Find button |
In addition we declare several private functions:
findFiles() |
Search for files matching the search parameters |
showFiles() |
Display the search result |
ceateButton() |
Construct the widget |
createComboBox() |
Construct the widget |
createFilesTable() |
Construct the widget |
Window Class Implementation▲
In the constructor we first create the application's widgets.
Window::
Window(QWidget *
parent)
:
QWidget(parent)
{
setWindowTitle(tr("Find Files"
));
QPushButton *
browseButton =
new
QPushButton(tr("&Browse..."
), this
);
connect(browseButton, &
amp;QAbstractButton::
clicked, this
, &
amp;Window::
browse);
findButton =
new
QPushButton(tr("&Find"
), this
);
conne