QFileSystemModel - iterating through current index children
-
How hard will it be to get a list of unchecked files from the root directory filtered by a certain extention.
For instance, you´ve filter the model to show all .txt file in the tree. I see that you have all of the checked .txt file listed in the snapshot shown above. How hard will it to be to get all the .txt file that is NOT checked from your model?
edit: The goal is to get a list of all UNCHECKED filtered files from -partially checked/checked- all directory.
-
Hmmm... That is actually not trivial. I guess you can do the filtering on .txt files using the QFileSystemModel itself, by using the setNameFilter.
In principle, getting the unchecked items from the tree is about the same complexity as getting the checked ones: doable. That is: you would get a list of unchecked files, and of unchecked branches.
However, it is not really feasable to get all the unchecked .txt files inside the unchecked branches. That would bring you back to having to populate the entire tree and potentially recurse through the whole file system to find them all. That would be outside the scope of this class, and should be done using QDirIterator, I think (if you really can't avoid this) or using system dependent methods if your system supports more efficient search of files than just scanning the complete file system. You can still use the results of the selection to limit the search scope though. -
You read my mind. It is very specific to my project. I have been researching and I think that QDirIterator along with your class will be the solution to my problem. I will basically use QDirIterator to get all my filtered files and compare it to your model/class result and determine if it is unchecked.. Thanks!
-
[quote author="phamtv" date="1304006640"]You read my mind. It is very specific to my project. I have been researching and I think that QDirIterator along with your class will be the solution to my problem. I will basically use QDirIterator to get all my filtered files and compare it to your model/class result and determine if it is unchecked.. Thanks![/quote]
I could add methods to get the unchecked files and branches like you can get the checked ones now. That would mean that you would only need to iterate over the branches returned by this method. That might be much quicker than iterating over the whole FS, and then comparing.
[quote author="phamtv" date="1304006828"]Just crossed my mind, will the class you´ve implemented support multiple selection check/uncheck propagation?[/quote]
I am not sure what you mean by that. Checking/unchecking all the selected files in one go?
Such a thing is not possible to support directly, as the selection is not part of the model. The model doesn't know about which items are selected. It could be done by simply setting the check state on all indexes in a selection though. -
bq. I could add methods to get the unchecked files and branches like you can get the checked ones now. That would mean that you would only need to iterate over the branches returned by this method. That might be much quicker than iterating over the whole FS, and then comparing.
That would be greatly appreciated! However, like you´ve said, wouldn´t that mean that if my initial state for all the dir and files were unchecked,
bq. That would bring you back to having to populate the entire tree and potentially recurse through the whole file system to find them all
-
[quote author="phamtv" date="1304007289"]bq. I could add methods to get the unchecked files and branches like you can get the checked ones now. That would mean that you would only need to iterate over the branches returned by this method. That might be much quicker than iterating over the whole FS, and then comparing.
That would be greatly appreciated! However, like you´ve said, wouldn´t that mean that if my initial state for all the dir and files were unchecked,
bq. That would bring you back to having to populate the entire tree and potentially recurse through the whole file system to find them all[/quote]
Yes, it would. But if you are interested in unchecked items, then it would probably make sense to make checked the default for all the nodes at the start. I don't think there is a way around to recursing through at least part of the file system in your case, and a worst-case scenario for that would be recursing through the whole file system.
-
I have created a "repository":https://gitorious.org/checkableproxymodel on gitorious for this project. The licence for this project is GPL-3. Note that CheckableProxyModel makes use of my "DelayedExecutionTimer class":http://developer.qt.nokia.com/wiki/Delay_action_to_wait_for_user_interaction that I published earlier on this site (BSD).
I have also made a separate "announcement":http://developer.qt.nokia.com/forums/viewthread/5531/ in the showcase section.
-
You're welcome. I always like a good challenge, and this question was quite interesting.
Please let me know what you think of the code, the API and inform me of any issues you run into with this code. Also, I hope you can follow how this actually works. If you enable the debug information, you can actually see of how many items the state is stored. You'll see that that is not so many...
Just out of curiousity: what application are you working on?
-
I am working on an application that will be the main interface for our engineers to add, delete, and edit test modules written in Expect/tcl. It will have the capability to execute and interact with an external process called dejagnu that will run our automated roadtest modules to test our embedded devices.
-
Hi Andre,
when I set the model of my QTreeView to the CheckableProxyModel, how can I determine if the QModelIndex I just double-clicked on is a Directory or not? My previous implementation prior to setting the model to CheckableProxyModel is as follow:
@
void TreeEditor::openFile(const QModelIndex& index)
{
if(index.isValid() && index.model())
{
QFileSystemModel* model = (QFileSystemModel*)index.model();if(!model->isDir(index)) { QString filePath = model->filePath(index); open(filePath); } }
}
@Do I have add a function to the CheckableProxyModel class to find out if the index I clicked on is a directory?
-
Keep using isDir, but map the item index before querying it. At least, that is how I would do it. I guess a branch node in the tree is not in all cases the same as a node representing a directory.
I would keep this functionality out of CheckableProxyModel. Just let that class do what it does. If you add this, you will bind it to be only usable in combination with a QFileSystemModel. That would be a shame.
@
void TreeEditor::openFile(const QModelIndex& index)
{
if(index.isValid() && index.model())
{
QFileSystemModel* model = m_fileSystemModel; // just keep a pointer around
CheckableProxyModel* proxy = m_checkableProxyModel; //same hereif(!model->isDir(proxy->mapToSource(index))) { QString filePath = index.data(QFileSystemModel::FilePathRole).toString(); open(filePath); } }
}
@ -
I understand that this is pertaining only to QFileSystemModel so I don´t want to add it to the CheckableProxyModel class, but how can I dynamically populate the QTreeView with only the directory I am interested in. My previous code to set the current directory as the root of the QTreeView was as follow:
@
QDir::setCurrent(strCurrTestDir);
QModelIndex idx = fsModel->index(strCurrTestDir);
fileTree->setRootIndex(idx);
@Resolved:
@
QDir::setCurrent(strCurrTestDir);
QModelIndex idx = fsModel->index(strCurrTestDir);
fileTree->setRootIndex(m_checkProxy->mapFromSource(idx));
@ -
Hi Andre,
your implementation of the Qtree´s checkstate propogation works great!!! Thank you!
One specific question related to my project is that since I am interested in getting the unchecked files for processing. What would be the quickest method for me to get all of the unchecked filenames under the unchecked directories? Should I be using QDirectoryIterator?