Problem with QFileSystemModel

54 views
Skip to first unread message

Benjam901

unread,
Nov 21, 2020, 8:49:43 AM11/21/20
to Python Programming for Autodesk Maya
Hello group,

It has been a long time since I've posted btu I have a problem that I need some help with in PySide2.

So, I have a QFileSystemModel set up as the model for a TreeView to list dirs in a system. What I am setting up for the user is for them to choose which dirs are listed, include favourites etc. What I am trying to achieve is for the test_duplicates folder to be the ONLY folder that is shown & expandable with all relevant filetypes that the user may want shown, audio files, image files etc.



When searching for a solution to this problem I cam across this old post: https://groups.google.com/g/python_inside_maya/c/a2QM_KvgfeI

I have implemented justins example but have tried to flip it so that ONLY a certain dir shows up instead of exluding the dir pattern I get an empty result set. When I debugged it, it looked like QFileSystemModel iterates the entire dir tree and excludes the root level folders too... is this expected behaviour?



Here is what I did, nothing mega really, just inverted the True/False for Justins model 
https://gist.github.com/ben-hearn-sb/ab2693db819248efea85ecef355e0887

Cheers!

Ben

Justin Israel

unread,
Nov 21, 2020, 4:10:01 PM11/21/20
to python_in...@googlegroups.com


On Sun, Nov 22, 2020, 2:49 AM Benjam901 <benandr...@gmail.com> wrote:
Hello group,

It has been a long time since I've posted btu I have a problem that I need some help with in PySide2.

So, I have a QFileSystemModel set up as the model for a TreeView to list dirs in a system. What I am setting up for the user is for them to choose which dirs are listed, include favourites etc. What I am trying to achieve is for the test_duplicates folder to be the ONLY folder that is shown & expandable with all relevant filetypes that the user may want shown, audio files, image files etc.



When searching for a solution to this problem I cam across this old post: https://groups.google.com/g/python_inside_maya/c/a2QM_KvgfeI

I have implemented justins example but have tried to flip it so that ONLY a certain dir shows up instead of exluding the dir pattern I get an empty result set. When I debugged it, it looked like QFileSystemModel iterates the entire dir tree and excludes the root level folders too... is this expected behaviour?

Yes the proxy model is generic so it isn't specifically aware of files vs directories and it going to just ask you if any row should be filtered out. 
The difference between your modification and the original code is that the original naively assumes directories won't match extensions and will always end up being shown. So it was just files with extensions thag would be subject to exclusion if they match the ext list. When you try and flip the logic you are now saying that even directories that don't have the desired ext should be excluded which means the children are gone too. 
What you might want to do is to check if the index is a directory first. 

    if sourceModel.isDir(idx):
        return True

This will get you part of the way there since it will now only exclude files that don't match. But you will be left with empty dirs. There could be a few ways to handle this. One is more manual work since it would require that you perform your own listing on the dir and check all the children to see if it's going to end up empty. If so then you want to return False so it will exclude the dir. Another option is to make the proxy model system do all the work and create a second proxy model that can run a filter pass after the first one removes all the files. That would then let you look for dirs and the source proxy model will tell you if it has children. 

Justin 





Here is what I did, nothing mega really, just inverted the True/False for Justins model 
https://gist.github.com/ben-hearn-sb/ab2693db819248efea85ecef355e0887

Cheers!

Ben

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b79c066f-d181-4e88-a250-d2f2654452f9n%40googlegroups.com.

Benjam901

unread,
Nov 22, 2020, 2:28:45 PM11/22/20
to Python Programming for Autodesk Maya
Hmm, that seems like a lot of manual work for something that should be quite simple.

At the moment I have a toggle button that hides/shows the QFileSystemModel. It works ok but it looks ugly and removes my ability to handle the user clicking on the top level dir. (I want to isolate the dir contents in my corresponding QSqlTableModel database).

I tried experimenting with a treewidget to hold the custom file system model. It worked but when you expand the tree you get this enormous selectable block to the left of it. You can test it out here and see what I mean:


How much slower would an entire tree model be than using QFileSystemModel? If that is the best way t get the results I want then I might look at heading that way

// Ben

Reply all
Reply to author
Forward
0 new messages