0

I want to traverse the drives present in my system and search for audio/video files inside it. Basically traverse the sub directories and display the file inside the treeview. I have 2 tree views, One to display System Directories and other to display audio/video files.

// Displays System Drives inside TreeView(Drive View) When Application Starts
void PanasonicViewer::onCamStartup()
{
    m_SystemModel = new QFileSystemModel(this);
    m_SystemListViewModel = new QFileSystemModel(this);
    m_SystemModel->setRootPath(QDir::currentPath());
    ui->DriveView->setModel(m_SystemModel); //Left side TreeView
    ui->DriveListView->setModel(m_SystemListViewModel); //Right Side TreeView

    // regard less how many columns you can do this using for:
    for(int nCount = 1; nCount < m_SystemModel->columnCount(); nCount++)
       ui->DriveView->hideColumn(nCount);
}

//On Clicking The TreeView, it should display Audio and Video files in DriveListView
void PanasonicViewer::on_DriveView_clicked(const QModelIndex &index)
{
    QStringList sDriveFilters;

    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 );

    sDriveFilters << "*.aac" << "*.wmv" << "*.avi" << "*.mpeg" << "*.mov" << "*.3gp" << "*.flv" << "*.mp3" ;

    m_SystemListViewModel->setNameFilters(sDriveFilters);
    m_SystemListViewModel->setNameFilterDisables(false);
}

You can notice in the above click event that I have set Filter to selected extensions. This seems to work and displays audio and video files when I click a drive i.e. E:\ but doesnt display the files present inside subfolders. Where am i going wrong?

StonedJesus
  • 397
  • 1
  • 5
  • 26

3 Answers3

0

The problem is due to what you're setting for QFileSystemModel::filter.

The default setting for is QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs, where QDir::AllEntries translates to QDir::Dirs | QDir::Files | QDir::Drives. If we expand it out, the default setting is:

QDir::Dirs | QDir::Files | QDir::Drives | QDir::NoDotAndDotDot | QDir::AllDirs

By calling m_SystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs ), you're telling the model that you only want to see standard directories, and want to exclude all files. Just remove your call to setFilter altogether and you should be fine.

sam-w
  • 7,478
  • 1
  • 47
  • 77
  • Thanks for the reply buddy. I removed `SetFilter` as mentioned above, but it doesnt work. Still doesnt display audio/video files :( – StonedJesus Nov 20 '12 at 06:54
0

Try changing

ui->DriveListView->setRootIndex(m_SystemListViewModel->setRootPath(sPath));

to

m_SystemListViewModel->setRootPath(sPath)
ui->DriveListView->setRootIndex(m_SystemListViewModel->index(sPath);

I'm skeptical whether the QModelIndex returned by setRootPath is the one you think it is. Troubleshoot this by removing the filter from m_SystemListViewModel to make sure that you're actually viewing the folder that you think you are.

I know that this should work if sPath is a directory path. I'm not sure if it'll work as expected if it's a file path.

Phlucious
  • 3,704
  • 28
  • 61
0

What you expect is not what the standard QFileSystemModel is supposed to do. What you need is to manually extract the multimedia files recursively in QStringListModel and set it on your QListView, or directly use QListWidget to add items directly while enumerating. When the selection over the QFileSystemModel changes you can get the path of the selected folder and enumerate the files and the subfolder files recursively. Note that if you click accedentally some drive you will end-up waiting for all the multimedia files in your drive. If that is what you want then maybe you may consider enumerating in worker thread which is not so easy.

Uga Buga
  • 1,724
  • 3
  • 19
  • 38