Qt 文件搜索

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值