Fails to display mp3 files in TreeView in Qt

2019-06-14 18:03发布

Alright I seem to have come across a situation where I need to traverse my directories and search for only .mp3 and .mpeg files. Once searched, I want to display them in my tree view.

Basically I have 2 tree views in my app, one displays the system directories and other should display only .mp3 and .mpeg files.

Here is the code

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

// Displays Files in Detail View on Clicking Drive
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" || QFileInfo(dirIt.filePath()).suffix() == "mpeg")
        {
            qDebug()<<dirIt.filePath();
            m_list<<dirIt.filePath();
            m_list.append(dirIt.filePath());
        }
    }

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

where DriveView is the treeview which displays drives and DriveListView is the treeview which should display only mp3 and mpeg files. When I debug the code, qDebug()<<dirIt.filePath(); gives me the path of all .mp3 and .mpeg files in Application Output tab but doesnt display them inside the DriveListView(treeview). As of now it displays only the files in the drive but the subfolders inside the drive also have mp3 files which are not displayed.

Here is the pic for reference: enter image description here

Notice there are many folders inside SONGS where mp3 files are present but it doesnt display.

Where am i making mistake???

2条回答
祖国的老花朵
2楼-- · 2019-06-14 18:30

May be try to use not QFileSystemModel to your m_SystemListViewModel, but create your own model which inherits QAbstractItemModel, and use QStyledItemDelegate over it.

查看更多
Emotional °昔
3楼-- · 2019-06-14 18:52

To me it looks like the call to m_SystemListViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot ); will explicitly tell Qt to only shows files.

According to the documentation, you should simply pass QDir::AllDirs with your previous filters.

QDir::AllDirs - List all directories; i.e. don't apply the filters to directory names.

查看更多
登录 后发表回答