QT中检索设定目录下所有指定文件的方法

void MainWindow::on_pushButton_clicked()
{
    QDir dir=QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                               "",
                                               QFileDialog::ShowDirsOnly
                                               | QFileDialog::DontResolveSymlinks);

    QStringList nameFilters;
    nameFilters << "*.c" << "*.h";
    QDirIterator dirIterator(dir.path(), nameFilters, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
    QStringList fileName;
    while(dirIterator.hasNext())
    {
        fileName << dirIterator.fileName();
        dirIterator.next();
    }
    if(fileName.empty())
        return;
    fileName.removeFirst();
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setRowCount(fileName.size());

    QStringList headers;
    headers << QStringLiteral("文件") << QStringLiteral("类型");
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    for(int i=0;i<fileName.size();i++)
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(iconText,fileName[i]));


}

  

posted @ 2016-03-18 17:55  Weyne  阅读(972)  评论(0编辑  收藏  举报