I have 2 tree views in my .ui. One treeview is DriveView and other is DriveListView. Now I have written a code which displays the drives of my system in `DriveView. I have done it as follows:
// Gets called when application starts
void DetailView::onCamStartup()
{
m_SystemModel = new QFileSystemModel(this);
m_SystemListViewModel = new QFileSystemModel(this);
m_SystemModel->setRootPath(QDir::currentPath());
ui->DriveView->setModel(m_SystemModel);
ui->DriveListView->setModel(m_SystemListViewModel);
// regard less how many columns you can do this using for:
for(int nCount = 1; nCount < m_SystemModel->columnCount(); nCount++)
ui->DriveView->hideColumn(nCount);
}
Now once i click a particular drive in my DriveView it shows me the subfolders inside it. What i basically need to do is to iterate/traverse the entire drive and search for .mp3 files inside. Basically check all folders and subfolders for it. Once it locates the files, it should display them in my another Treeview i.e. DriveListView. I have written a following code for it:
void DetailView::on_DriveView_clicked(const QModelIndex &index)
{
QString sPath = m_SystemModel->fileInfo(index).absoluteFilePath();
ui->DriveListView->setRootIndex(m_SystemListViewModel->setRootPath(sPath));
m_SystemModel->setRootPath(QDir::currentPath());
m_SystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs );
m_SystemListViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot );
QStringList m_list;
QDirIterator dirIt(sPath,QDirIterator::Subdirectories);
while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
{
if (QFileInfo(dirIt.filePath()).suffix() == "mp3")
{
qDebug()<<dirIt.filePath();
m_list<<dirIt.filePath();
m_list.append(dirIt.filePath());
}
}
QStringListModel *model = new QStringListModel();
model->setStringList(m_list);
m_SystemListViewModel->setNameFilterDisables(false);
}
}
Whenever I click the subfolders which have mp3 files, it displays them in treeview. This is were I am facing the problem. When I click the mail folder, nothing gets displayed. Ideally I want to display all the mp3 files present in main folder and its subfolders to be displayed. I have put qDebug()<<dirIt.filePath(); and when i run the app and click a drive, this prints me all the .mp3 files in Application Output but it doesnt display them in the treeview i.e. DriveListView.
Since you are iterating over all subdirs manually anyway, try using a
QStringListModelform_SystemListViewModel. This way, you can directly displaym_listin the view. See http://qt-project.org/doc/qt-4.8/qstringlistmodel.htmlAlternatively, it looks like a
QSortFilterProxyModelmight be used to filter the displayed results in the list view, but I have never used it. See http://qt-project.org/doc/qt-4.8/qsortfilterproxymodel.html