无法在Qt中的TreeView中显示mp3文件 [英] Fails to display mp3 files in TreeView in Qt
问题描述
好吧,我似乎遇到了一种情况,我需要遍历我的目录,只搜索.mp3和.mpeg文件。一旦搜索,我想在我的树视图中显示它们。
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.
基本上我有两个树视图在我的应用程序,一个显示系统目录,其他应该只显示.mp3和.mpeg文件。
Basically I have 2 tree views in my app, one displays the system directories and other should display only .mp3 and .mpeg files.
下面是代码
// 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);
}
}
其中 DriveView
是显示驱动器的树视图, DriveListView
是应该只显示mp3和mpeg文件的树视图。当我调试代码, qDebug()<
应用程序输出
选项卡,但不会在DriveListView(treeview)中显示它们。到目前为止,它只显示驱动器中的文件,但驱动器中的子文件夹也有mp3文件,不显示。
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:
请注意,SONGS中有许多文件夹,其中有mp3文件,但不显示。
Notice there are many folders inside SONGS where mp3 files are present but it doesnt display.
我在做错误
推荐答案
可以尝试使用 QFileSystemModel
到你的 m_SystemListViewModel
,但创建你自己的模型,继承 QAbstractItemModel
,并使用 QStyledItemDelegate
。
May be try to use not QFileSystemModel
to your m_SystemListViewModel
, but create your own model which inherits QAbstractItemModel
, and use QStyledItemDelegate
over it.
这篇关于无法在Qt中的TreeView中显示mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!