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])); }