PDA

View Full Version : Get names of filtered files,folders in list using QFileSystemModel,QSortFilterProxymo



Dev
28th October 2013, 09:39
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);
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(QSt ring)),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());
}
}
}
}
}

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.

anda_skoa
28th October 2013, 12:24
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,
_

Dev
29th October 2013, 03:14
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.

anda_skoa
29th October 2013, 07:48
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,
_

Dev
29th October 2013, 08:45
thanks,
Its my updated code.First half of the code is working fine.


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

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.

anda_skoa
29th October 2013, 12:31
Ah, but the problem is in the other part of the code.

See, for the view you are using


ui->treeView_2->setModel(proxyModel);


But for the slot you are using


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,
_

Dev
15th November 2013, 09:40
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 ?

anda_skoa
15th November 2013, 17:20
Just to be sure: your onDirectoryLoaded() now also uses the proxyModel instance?

Cheers,
_