Qt 文件搜索

摘要:
该代码是在互联网上偶然发现的。下载后,它更改了几种格式。编译后,测试后可以正常使用。这是一个很好的文件搜索示例。有两种搜索方法:一种是按文件名作为关键字进行搜索,另一种是按照文档中包含的关键字进行搜索。首先,粘贴两张图片:上图显示了两个文本文档,其中都包含“2010-12-05”等关键字。您可以稍后使用此关键字进行搜索,看看是否有效…注意:此实例程序无法执行递归搜索,只能搜索指定目录级别的文件。此外,它只能匹配文本文档。

代码无意间在网上找到,下载回来后改了几个格式,编译后经测试可以正常使用,这个个文件搜索的很好的例子,有两种搜索方式:一种是按文件名作为关键字进行搜索,一种是以文档中所包含的关键字进行搜索,贴两张图先:

Qt 文件搜索第1张

上图为两个文本文档(都位于C盘根目录下),其中都包含有"2010-12-05"这样的关键字,一会就用这个关键字搜索看看行不行...

Qt 文件搜索第2张

注意:这个实例程序无法进行递归的查找,只能搜索指定目录这一层的文件,另外它只能匹配文本文档。

1 #ifndef WINDOW_H
2 #defineWINDOW_H3 4 #include<QDialog>5 classQComboBox;
6 classQDir;
7 classQLabel;
8 classQPushButton;
9 classQTableWidget;
10 11 classWindow:publicQDialog
12 {
13 Q_OBJECT
14 public:
15 Window(QWidget *parent=0);
16 privateslots:
17 voidbrowse();
18 voidfind();
19 private:
20 QStringList findFiles(constQDir &directory,constQStringList &files,
21 constQString &text);
22 voidshowFiles(constQDir &directory,constQStringList &files);
23 QPushButton *createButton(constQString &text,constchar*member);
24 QComboBox *createComboBox(constQString &text =QString());
25 voidcreateFilesTable();
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 #endif39
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 =newQLabel(tr("Named:"));
12 textLabel =newQLabel(tr("Containing text:"));
13 directoryLabel =newQLabel(tr("In directory:"));
14 filesFoundLabel =newQLabel;
15 createFilesTable();
16 QHBoxLayout *buttonsLayout =newQHBoxLayout;
17 buttonsLayout->addStretch();
18 buttonsLayout->addWidget(findButton);
19 QGridLayout *mainLayout =newQGridLayout;
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 voidWindow::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 voidWindow::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(constQDir &directory,constQStringList &files,
59 constQString &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(inti =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 returnfoundFiles;
88 }
89 voidWindow::showFiles(constQDir &directory,constQStringList &files)
90 {
91 for(inti =0; i <files.size(); ++i) {
92 QFile file(directory.absoluteFilePath(files[i]));
93 qint64 size =QFileInfo(file).size();
94 QTableWidgetItem *fileNameItem =newQTableWidgetItem(files[i]);
95 fileNameItem->setFlags(Qt::ItemIsEnabled);
96 QTableWidgetItem *sizeItem =newQTableWidgetItem(tr("%1 KB")
97 .arg(int((size +1023) /1024)));
98 sizeItem->setTextAlignment(Qt::AlignRight |Qt::AlignVCenter);
99 sizeItem->setFlags(Qt::ItemIsEnabled);
100 introw =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(constQString &text,constchar*member)
108 {
109 QPushButton *button =newQPushButton(text);
110 connect(button,SIGNAL(clicked()),this,member);
111 returnbutton;
112 }
113 QComboBox *Window::createComboBox(constQString &text)
114 {
115 QComboBox *comboBox =newQComboBox;
116 comboBox->setEditable(true);
117 comboBox->addItem(text);
118 comboBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
119 returncomboBox;
120 }
121 voidWindow::createFilesTable()
122 {
123 filesTable =newQTableWidget(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 intmain(intargc,char*argv[])
5 {
6 QApplication app(argc,argv);
7 Window window;
8 window.show();
9 returnapp.exec();
10 }
11

免责声明:文章转载自《Qt 文件搜索》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux 下 FreeSWITCH 远程连接使用 MySQL 替代 SQLite关于宝塔一个站点绑定多个域名宝塔ssl证书的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mac everything的替代品——fzf使用,速度还是很快的!!!

fzf模糊搜索神器的安装和使用 fzf是一个通用的命令行模糊查找器, 通过输入模糊的关键词就可以定位文件或文件夹。结合其他工具(比如rg)可以完成非常多的工作,在工作中可以大幅提高你的工作效率。 fzf可以用于文件、命令历史记录、进程、主机名、书签、git提交等。 1. fzf使用 1.1 安装 Using Homebrew You can use...

apache2添加模块和添加站点

apache2添加模块和添加站点 linux下的apache2的目录和windows上的区别还是很大的,但是用起来却更方便了,详解请看另一篇文章http://www.cnblogs.com/wancy86/p/linux_apache2.html 这里补充两个命令: 添加模块: model_name是mods-available 下的模块名 sudo a2...

JBOSS目录结构详细说明

一、下载与安装。 如何下载以及安装配置,请参考:Windows下JBOSS安装配置图文教程 二、现在主要了解一下JBOSS目录结构。 1. 主目录: E:jboosjboss-6.1.0.Final bin 开始和停止JBoss的地方。 其中有两个主要的批处理文件:run.bat和shutdown.bat。要启动JBoss只要执行run.bat文件即可;...

在Windows Server 2012下玩扫雷

由于受到了DreamSpark学生认证,我用的是Windows Server 2012的正版系统,但有一个问题就是,这个系统不能玩扫雷。 经过探索,在MSDN论坛上找到解决办法,原文如下:http://social.technet.microsoft.com/Forums/zh-CN/winserver8gen/thread/b64cdc31-7e2d-4...

索引的删除和更新

【删除索引】 1 /** 2 * 测试删除索引 3 */ 4 @Test 5 public void testDelete(){ 6 IndexWriter writer = null; 7 8 try { 9...

使用TinyPNG提供的API,对图片进行压缩(C#)

项目需要,经常需要手动压缩图片,流程太过麻烦,效率低下。所以写了一个小程序,以提高工作效率 using System; using System.Net; using System.Text; using System.IO; classProgram { staticvoidMain() { Console.WriteLine("请输入TinyPn...