Get names of filtered files,folders in list using QFileSystemModel,QSortFilterProxymo
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:
Code:
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());
}
}
}
}
}
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.
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
Not sure what your problem is.
The slot works with the QFIleSystemModel instance which of course doesn't know anything about things the QSortFilterProxyModel does.
If you want to get the things the view sees, you have to get the data from the same model that the view uses.
Cheers,
_
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
Thanks for your response, I am doing the same as you are saying. I am trying to get the names from the same model but i think i am missing something. Can you tell me how to use the data of that model ? FYI, my problem is I want filtered names list of only those files and folders which have a particular type of file extension(*.mp3,*.jpg).
Really appreciate if you could help me out of this.
Thanks.
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
Quote:
Originally Posted by
Dev
Thanks for your response, I am doing the same as you are saying. I am trying to get the names from the same model but i think i am missing something.
The code you posted used different models. The view uses a filter proxy model, the slot uses the file system model.
If your current code is different and you use the filtered data in both places and it still does not work, please post updated code.
Cheers,
_
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
thanks,
Its my updated code.First half of the code is working fine.
Code:
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));
after this i am trying to fill the names of files and folders in the model. so i am calling a signal directoryloaded() for files and folders names from the model and the model name is fileSystemModel. Thats i am using that model in other part of my code. Am i on right way ? If not then tell me what is the proper solution for getting names. Thanks.
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
Ah, but the problem is in the other part of the code.
See, for the view you are using
Code:
ui->treeView_2->setModel(proxyModel);
But for the slot you are using
Code:
int rowCount = fileSystemModel->rowCount(index);
Are those the same? I.e. is that the same pointer just with different names in different code parts?
Cheers,
_
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
thanks, i have done it. it works fine.
But In my second part of the code, i am not getting the filtered names of the files and subfolders as my treeview is displaying.
Can you please tell what is problem behind this ?
Re: Get names of filtered files,folders in list using QFileSystemModel,QSortFilterPro
Just to be sure: your onDirectoryLoaded() now also uses the proxyModel instance?
Cheers,
_