I am using QFileSystemModel for exploring my drive and QSortFilterProxyModel for filtering all the files and folders in it. It works well when i try to display the model in a QTreeview. Now my problem is, when i try to get all the files and folders names in a list. It gives me unfiltered names. I am using this:
QFileSystemModel *fileSystemModel;
fileSystemModel = new QFileSystemModel(this);
QModelIndex rootModelIndex
= fileSystemModel
->setRootPath
(path
);
filters <<"*.jpeg" <<"*.pict" ;
fileSystemModel->setNameFilters(filters);
fileSystemModel->setNameFilterDisables(0);
proxyModel->setSourceModel(fileSystemModel);
fileSystemModel->fetchMore(rootModelIndex);
fileSystemModel->sort(0,Qt::AscendingOrder);
ui->treeView_2->setModel(proxyModel);
ui->treeView_2->setRootIndex(proxyModel->mapFromSource(rootModelIndex));
connect(fileSystemModel,
SIGNAL(directoryLoaded
(QString)),
this,
SLOT(onDirLoaded
(QString)));
for names list i am using the Directoryloaded() signal of QFileSystemModel:
void MainWindow
::onDirLoaded(QString path
) {
int rowCount = fileSystemModel->rowCount(index);
for(int i=0;i<rowCount;i++)
{
QFileInfo fileInfo
= fileSystemModel
->fileInfo
(mi
);
if (!templist.contains(fileInfo.absoluteFilePath()))
{
templist << fileInfo.absoluteFilePath();
tempNamelist << fileInfo.fileName();
}
if(!bloaded)
{
if(fileSystemModel->hasChildren(mi))
{
QModelIndex index1
= fileSystemModel
->index
(i,
0,index
);
int rowCount1 = fileSystemModel->rowCount(index1);
if(rowCount1)
{
onDirLoaded(fileInfo.absoluteFilePath());
}
}
}
}
}
QFileSystemModel *fileSystemModel;
fileSystemModel = new QFileSystemModel(this);
QSortFilterProxyModel proxyModel = new QSortFilterProxyModel(targetDir);
QModelIndex rootModelIndex = fileSystemModel->setRootPath(path);
QStringList filters;
filters <<"*.jpeg" <<"*.pict" ;
fileSystemModel->setNameFilters(filters);
fileSystemModel->setNameFilterDisables(0);
proxyModel->setSourceModel(fileSystemModel);
fileSystemModel->fetchMore(rootModelIndex);
fileSystemModel->sort(0,Qt::AscendingOrder);
ui->treeView_2->setModel(proxyModel);
ui->treeView_2->setRootIndex(proxyModel->mapFromSource(rootModelIndex));
connect(fileSystemModel,SIGNAL(directoryLoaded(QString)),this,SLOT(onDirLoaded(QString)));
for names list i am using the Directoryloaded() signal of QFileSystemModel:
void MainWindow::onDirLoaded(QString path)
{
QModelIndex index = fileSystemModel->index(path);
int rowCount = fileSystemModel->rowCount(index);
for(int i=0;i<rowCount;i++)
{
QModelIndex mi = fileSystemModel->index(i,0,index);
QFileInfo fileInfo = fileSystemModel->fileInfo(mi);
if (!templist.contains(fileInfo.absoluteFilePath()))
{
templist << fileInfo.absoluteFilePath();
tempNamelist << fileInfo.fileName();
}
if(!bloaded)
{
if(fileSystemModel->hasChildren(mi))
{
QModelIndex index1 = fileSystemModel->index(i,0,index);
int rowCount1 = fileSystemModel->rowCount(index1);
if(rowCount1)
{
onDirLoaded(fileInfo.absoluteFilePath());
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
After this i am getting list of all the files and folders but its unfiltered names.I want filtered names. Is there anyone who can help me in this ? Your help will really appreciate guys.
Thanks.
Bookmarks