PDA

View Full Version : Select all files [QTableView et QFileSystemModel]



snooker9
23rd December 2011, 09:17
Hi!

I need to select all files from a QTableView, which has for model a QFileSystemModel.
The problem is that my QTableView displays the files and folders, so i need to select only the files.

This following Qt function selects the files and folders : it's why I cannot use it directly.


tableView->selectAll();


I have written a function which parse all elements from a QTableView and selects only the files.


//I get the modelIndex root
QModelIndex rootIndex = tableView->rootIndex();
//If the model is not valid, i cancel
if(false == rootIndex.isValid())
return;
//I parse all lines from my root directory
for(int i = 0 ; i < fileSystemModel->rowCount(rootIndex) ; i++)
{
//I get the current line
QModelIndex currentIndex = fileSystemModel->index(i, 0, rootIndex);
//If the current modelIndex is valid, i continue
if(true == currentIndex.isValid())
//If the current modelIndex is not a directory
if(false == fileSystemModel->fileInfo(currentIndex).isDir())
//If the current modelIndex is not already selected
if(false == tableView->selectionModel()->isSelected(currentIndex))
//I select it
tableView->setCurrentIndex(currentIndex);
}


This code seems ok. But, when i select a lot of files (for example 600), this function is very very slow...
It's much more slow than tableView->selectAll().

Have you got a solution to :
- optimize my function?
- or a easier way to select all files?

I thank you in advance!