如何从QT中的QFileSystemModel获取文件路径(C++)
首先,假设存在一个存储多个图像的文件夹。然后,我尝试单击UI中的按钮打开文件夹,然后将该文件夹中所有图像的文件路径保存到QList(仅限过滤的图像文件)。但QList不存储任何东西。请帮忙。
void MainWindow::on_pushButton_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
model = new QFileSystemModel();
filesPath = dir;
model->setRootPath(dir);
QStringList filter;
filter <<"*.png" <<"*.jpg" <<"*.bmp" <<"*.gif";
model->setNameFilters(filter);
model->setNameFilterDisables(false);
ui->treeView->setModel(model);
ui->treeView->setRootIndex(model->index(dir));
ui->treeView->setAnimated(false);
ui->treeView->setSortingEnabled(true);
QList<QString> path_list;
QModelIndex parentIndex = model->index(dir);
int numRows = model->rowCount(parentIndex);
for (int row = 0; row < numRows; ++row) {
QModelIndex childIndex = model->index(row, 0, parentIndex);
QString path = model->data(childIndex).toString();
if(!QFileInfo(path).isDir())
path_list.append(path);
}
}