Qt 文件搜索(写入文本文件)
代码无意间在网上找到,下载回来后改了几个格式,编译后经测试可以正常使用,这个个文件搜索的很好的例子,有两种搜索方式:一种是按文件名作为关键字进行搜索,一种是以文档中所包含的关键字进行搜索,贴两张图先:
上图为两个文本文档(都位于C盘根目录下),其中都包含有"2010-12-05"这样的关键字,一会就用这个关键字搜索看看行不行...
注意:这个实例程序无法进行递归的查找,只能搜索指定目录这一层的文件,另外它只能匹配文本文档。

1 #ifndef WINDOW_H
2 #define WINDOW_H
3
4 #include<QDialog>
5 class QComboBox;
6 class QDir;
7 class QLabel;
8 class QPushButton;
9 class QTableWidget;
10
11 class Window:public QDialog
12 {
13 Q_OBJECT
14 public:
15 Window(QWidget *parent=0);
16 private slots:
17 void browse();
18 void find();
19 private:
20 QStringList findFiles(const QDir &directory,const QStringList &files,
21 const QString &text);
22 void showFiles(const QDir &directory,const QStringList &files);
23 QPushButton *createButton(const QString &text,const char *member);
24 QComboBox *createComboBox(const QString &text = QString());
25 void createFilesTable();
26 QComboBox *fileComboBox;
27 QComboBox *textComboBox;
28 QComboBox *directoryComboBox;
29 QLabel *fileLabel;
30 QLabel *textLabel;
31 QLabel *directoryLabel;
32 QLabel *filesFoundLabel;
33 QPushButton *browseButton;
34 QPushButton *findButton;
35 QTableWidget *filesTable;
36 };
37
38 #endif
39


1 #include <QtGui>
2 #include "window.h"
3 Window::Window(QWidget *parent)
4 : QDialog(parent)
5 {
6 browseButton = createButton(tr("&Browse..."),SLOT(browse()));
7 findButton = createButton(tr("&Find"),SLOT(find()));
8 fileComboBox = createComboBox(tr("*"));
9 textComboBox = createComboBox();
10 directoryComboBox = createComboBox(QDir::currentPath());
11 fileLabel = new QLabel(tr("Named:"));
12 textLabel = new QLabel(tr("Containing text:"));
13 directoryLabel = new QLabel(tr("In directory:"));
14 filesFoundLabel = new QLabel;
15 createFilesTable();
16 QHBoxLayout *buttonsLayout = new QHBoxLayout;
17 buttonsLayout->addStretch();
18 buttonsLayout->addWidget(findButton);
19 QGridLayout *mainLayout = new QGridLayout;
20 mainLayout->addWidget(fileLabel,0,0);
21 mainLayout->addWidget(fileComboBox,0,1,1,2);
22 mainLayout->addWidget(textLabel,1,0);
23 mainLayout->addWidget(textComboBox,1,1,1,2);
24 mainLayout->addWidget(directoryLabel,2,0);
25 mainLayout->addWidget(directoryComboBox,2,1);
26 mainLayout->addWidget(browseButton,2,2);
27 mainLayout->addWidget(filesTable,3,0,1,3);
28 mainLayout->addWidget(filesFoundLabel,4,0);
29 mainLayout->addLayout(buttonsLayout,5,0,1,3);
30 setLayout(mainLayout);
31 setWindowTitle(tr("Find Files"));
32 resize(700,300);
33 }
34 void Window::browse()
35 {
36 QString directory = QFileDialog::getExistingDirectory(this,
37 QObject::tr("Find Files"),QDir::currentPath());
38 if (!directory.isEmpty()) {
39 directoryComboBox->addItem(directory);
40 directoryComboBox->setCurrentIndex(directoryComboBox->currentIndex() + 1);
41 }
42 }
43 void Window::find()
44 {
45 filesTable->setRowCount(0);
46 QString fileName = fileComboBox->currentText();
47 QString text = textComboBox->currentText();
48 QString path = directoryComboBox->currentText();
49 QDir directory = QDir(path);
50 QStringList files;
51 if (fileName.isEmpty()) fileName = "*";
52 files = directory.entryList(QStringList(fileName),
53 QDir::Files | QDir::NoSymLinks);
54 if (!text.isEmpty())
55 files = findFiles(directory,files,text);
56 showFiles(directory,files);
57 }
58 QStringList Window::findFiles(const QDir &directory,const QStringList &files,
59 const QString &text)
60 {
61 QProgressDialog progressDialog(this);
62 progressDialog.setCancelButtonText(tr("&Cancel"));
63 progressDialog.setRange(0,files.size());
64 progressDialog.setWindowTitle(tr("Find Files"));
65 QStringList foundFiles;
66 for (int i = 0; i < files.size(); ++i) {
67 progressDialog.setValue(i);
68 progressDialog.setLabelText(tr("Searching file number %1 of %2...")
69 .arg(i).arg(files.size()));
70 qApp->processEvents();
71 if (progressDialog.wasCanceled()) break;
72 QFile file(directory.absoluteFilePath(files[i]));
73 if (file.open(QIODevice::ReadOnly)) {
74 QString line;
75 QTextStream in(&file);
76
77 while (!in.atEnd()) {
78 if (progressDialog.wasCanceled()) break;
79 line = in.readLine();
80 if (line.contains(text)) {
81 foundFiles << files[i];
82 break;
83 }
84 }
85 }
86 }
87 return foundFiles;
88 }
89 void Window::showFiles(const QDir &directory,const QStringList &files)
90 {
91 for (int i = 0; i < files.size(); ++i) {
92 QFile file(directory.absoluteFilePath(files[i]));
93 qint64 size = QFileInfo(file).size();
94 QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
95 fileNameItem->setFlags(Qt::ItemIsEnabled);
96 QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
97 .arg(int((size + 1023) / 1024)));
98 sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
99 sizeItem->setFlags(Qt::ItemIsEnabled);
100 int row = filesTable->rowCount();
101 filesTable->insertRow(row);
102 filesTable->setItem(row,0,fileNameItem);
103 filesTable->setItem(row,1,sizeItem);
104 }
105 filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()));
106 }
107 QPushButton *Window::createButton(const QString &text,const char *member)
108 {
109 QPushButton *button = new QPushButton(text);
110 connect(button,SIGNAL(clicked()),this,member);
111 return button;
112 }
113 QComboBox *Window::createComboBox(const QString &text)
114 {
115 QComboBox *comboBox = new QComboBox;
116 comboBox->setEditable(true);
117 comboBox->addItem(text);
118 comboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
119 return comboBox;
120 }
121 void Window::createFilesTable()
122 {
123 filesTable = new QTableWidget(0,2);
124 QStringList labels;
125 labels << tr("File Name") << tr("Size");
126 filesTable->setHorizontalHeaderLabels(labels);
127 filesTable->horizontalHeader()->setResizeMode(0,QHeaderView::Stretch);
128 filesTable->verticalHeader()->hide();
129 filesTable->setShowGrid(false);
130 }
131


1 #include "window.h"
2 #include <QApplication>
3
4 int main(int argc,char *argv[])
5 {
6 QApplication app(argc,argv);
7 Window window;
8 window.show();
9 return app.exec();
10 }
11

http://www.cnblogs.com/hicjiajia/archive/2010/12/05/1896823.html
Qt 文件搜索(写入文本文件)的更多相关文章
- Android 建立文件夹、生成文件并写入文本文件内容
一.首先添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">& ...
- 项目实战:Qt文件改名工具 v1.2.0(支持递归检索,搜索:模糊匹配,前缀匹配,后缀匹配;重命名:模糊替换,前缀追加,后缀追加)
需求 在整理文件和一些其他头文件的时候,需要对其名称进行整理和修改,此工具很早就应该写了,创业后,非常忙,今天抽空写了一个顺便提供给学习. 工具和源码下载地址 本篇文章的应用包和源码包可在 ...
- Qt 文件处理(readLine可以读取char[],并且有qSetFieldWidth qSetPadChar 等全局函数)
Qt 文件处理 Qt提供了QFile类来进行文件处理,为了更方便地处理文本文件或二进制文件,Qt还提了QTextStream类和QDataStream类,处理临时文件可以使用QTemporaryFil ...
- Android 下载文件及写入SD卡
Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- python 将字节写入文本文件
想在文本模式打开的文件中写入原始的字节数据 将字节数据直接写入文件的缓冲区即可 >>> import sys >>> sys.stdout.write(b'Hell ...
- C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,支持doc,xls,ppt,pdf,txt等格式的文件搜索
C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,指定目录后,遍历搜索文件和子目,现在只写了支持.DOC.DOCX.XLS.XLSX.PPT.PPTX.PDF.HTML.HTM.TXT等 ...
- C# 实现写入文本文件内容功能
private void write_txt(string str1, string str2, string str3) { System.DateTime currentTime = System ...
- J2SE 8的输入输出--读取/写入文本文件和读取/写入二进制数据
读取/写入文本文件 // 1. 文本输入 // (1) 短小文本直接转入字符串 String string = new String(Files.readAllBytes(Paths.get(&quo ...
- 【MATLAB】读取和写入文本文件
在MATLAB中,来读取和写入文本文件是很简单的事.下面,就来简单介绍下.如果有其他问题,请留言. 一.读取文本文件 思路: 1.用fopen来打开一个文件句柄 2.用fgetl来获得文件中的一行,如 ...
随机推荐
- hdu 3832 Earth Hour
http://acm.hdu.edu.cn/showproblem.php?pid=3832 #include <cstdio> #include <iostream> #in ...
- LeetCode_Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- sign starfieldtech
signtool sign /f certfile.pfx /p password /tr http://tsa.starfieldtech.com /td SHA256 mycode.exe htt ...
- 正确地黑C
转载:http://tieba.baidu.com/p/3190068223?pn=1 本版暂时当作提纲,不做详细展开讨论,以后可能更新. 注意:本文主旨不是政治正确. 1.设计C的设计相对于同期来说 ...
- java设计模式--行为型模式--模板方法
什么是模板方法,这个有待考虑,看下面: 模板方法 概述 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步 ...
- 自定义ScrollViewer的Touch事件--触摸上下移动ScrollViewer滚动到指定位置
double mPointY;//触摸点的Y坐标 double mOffsetY;//滚动条当前位置 bool mIsTouch = false;//是否触摸 //触摸事件 private void ...
- Android核心基础(十)
1.音频采集 你可以使用手机进行现场录音,实现步骤如下: 第一步:在功能清单文件AndroidManifest.xml中添加音频刻录权限: <uses-permission android:na ...
- Hive 3、Hive 的安装配置(本地derby模式)
这种方式是最简单的存储方式,只需要在hive-site.xml做如下配置便可; $ vim hive-site.xml <configuration> <property> ...
- 《Java程序员面试笔试宝典》之 instanceof有什么作用
instanceof是Java语言中的一个二元运算符,它的作用是判断一个引用类型的变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例,即它左边的对象是否是它右边的类的实例,返回boolean类 ...
- C# CheckedListBox控件的使用方法
1. 加入项 checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checked ...