首先,假設存在一個存儲多個圖像的文件夾。然後,我嘗試單擊UI中的按鈕打開文件夾,然後將該文件夾中所有圖像的文件路徑保存到QList(僅限過濾的圖像文件)。但QList不存儲任何東西。請幫忙。如何從QT中的QFileSystemModel獲取文件路徑(C++)
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);
}
}
你需要閱讀Qt文檔:http://doc.qt.io/qt-5/qfilesystemmodel.html#details –
@ md612爲什麼你使用'QFileSystemModel'? –
@Dmitry Sazonov將其與QtreeView – md612