简述
如图递归获取文件夹内所有文件和子文件
代码
void findAllFiles(const QString path, QFileInfoList &fileInfoList)
{
QDir dir(path);
foreach(QFileInfo info,dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs)){
qDebug()<<"dir:"<<info.filePath();
findAllFiles(info.filePath(),fileInfoList);
}
foreach(QFileInfo info,dir.entryInfoList(QDir::Files)){
fileInfoList.append(info);
qDebug()<<"fileInfo"<<info;
}
}
参考测试代码
void MainWindow::on_pushButton_clicked()
{
m_fileInfoList.clear();
findAllFiles(ui->lineEdit->text(),m_fileInfoList);
QAbstractItemModel *pModel = ui->tableView->model();
if(!pModel){
pModel->deleteLater();
pModel = nullptr;
}
QStandardItemModel *pStandModel = new QStandardItemModel(m_fileInfoList.count(),3);
for (int row = 0; row < pStandModel->rowCount(); ++row) {
QStandardItem *item = new QStandardItem(m_fileInfoList[row].fileName());
pStandModel->setItem(row, 0, item);
QStandardItem *item1 = new QStandardItem(m_fileInfoList[row].absolutePath());
pStandModel->setItem(row, 1, item1);
QStandardItem *item2 = new QStandardItem(QString::number(m_fileInfoList[row].size()));
pStandModel->setItem(row, 2, item2);
}
ui->tableView->setModel(pStandModel);
}