0

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781

1 Answers1

0

Since you are iterating over all subdirs manually anyway, try using a QStringListModel for m_SystemListViewModel. This way, you can directly display m_list in the view. See http://qt-project.org/doc/qt-4.8/qstringlistmodel.html

Alternatively, it looks like a QSortFilterProxyModel might 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

Ron Kluth
  • 51
  • 3
  • Well when I use `QStringListModel`, it displays all the .mp3 files but only when the folders are clicked. What I mean to say is `Songs--> Folder1, Folder 2.` If i click `Folder1` it displays mp3 files in it, similarly if I click `Folder2` it shows mp3 files. But when i click `Songs` nothing gets displayed. Ideally I would want all the files present in Folder1, folder2 etc to be displayed when I click `Songs` folder. You understood what i am trying to say? :) – Owais Nabi Wani Nov 22 '12 at 06:45
  • In `on_DriveView_clicked` you are now creating a new `QStringModel` every time, but you are not assigning it to a view. Try `m_SystemListViewModel = new QStringListModel(this)` in `onCamStartup` and then set the string list in `on_DriveView_clicked`:`m_SystemListViewModel->setStringList(m_list);` – Ron Kluth Nov 22 '12 at 15:02
  • so you mean I need to set m_SystemListViewModel as `QStringListModel` in header file or what??? I tried doing it but throws error `cannot convert 'QStringListModel*' to 'QFileSystemModel*' in assignment` and `'class QFileSystemModel' has no member named 'setStringList'` since its of type QFileSystemModel – Owais Nabi Wani Nov 22 '12 at 15:17
  • Yes, you need to change the header declaration to `QStringListModel*` as well. The point is that you only need to initialize and assign the model to a view once and then update the model's contents whenever a directory is selected. This way, you will see all the files you found in the subdirectories and stored in `m_list`. – Ron Kluth Nov 23 '12 at 10:37