Good evening,
I am developing an application where I have to process a bunch of image files.
To load the file's list, the user have to press a load button and a QFileDialog is created, then I populate a QStringList model with the file's paths.
To show the list I use a QListView
Here the code:
fileDialog.setWindowTitle(tr("Open Directory"));
if (fileDialog.
exec() == QDialog::Accepted) {
QStringList files
= getImageFiles
(fileDialog.
selectedFiles().
first());
// Populate the model
m_model->setStringList(files);
// and pass it to the view
ui->listView->setModel(m_model);
}
QStringList MainWindow
::getImageFiles(const QString
& path
) const {
foreach (const QString& dir, dirs)
{
result.append(getImageFiles(root.filePath(dir)));
}
foreach (const QString& file, files)
{
result.append(root.filePath(file));
}
return result;
}
QFileDialog fileDialog;
fileDialog.setWindowTitle(tr("Open Directory"));
fileDialog.setFileMode(QFileDialog::DirectoryOnly);
if (fileDialog.exec() == QDialog::Accepted)
{
QStringList files = getImageFiles(fileDialog.selectedFiles().first());
// Populate the model
m_model->setStringList(files);
// and pass it to the view
ui->listView->setModel(m_model);
}
QStringList MainWindow::getImageFiles(const QString& path) const
{
QStringList result;
QDir root(path);
QStringList dirs = root.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
foreach (const QString& dir, dirs)
{
result.append(getImageFiles(root.filePath(dir)));
}
QStringList files = root.entryList(QString("*.jpg").split(";"), QDir::Files, QDir::Name);
foreach (const QString& file, files)
{
result.append(root.filePath(file));
}
return result;
}
To copy to clipboard, switch view to plain text mode
Now in the view I have a list of full paths, but I would only show the files names instead of full path while keeping the full paths in the model
How can I do it? I would have an help on it.
Thanx in advance
Bookmarks