 Hey everybody this is Brian and welcome to the 49th Qtutorial with C++ and GUI programming. Today we're going to cover the Q file system model. We're going to do something a little different with this today. So if you haven't already go out and look up the Q file system model class reference and kind of get familiar with this class. And if you've watched my last tutorial, we covered the Q-Dur model and a tree view and how to link those together. What is the difference between the Q file system model and the Q-Dur model? Well, they're both models. They both use the underlying file system to get their data. But the Q file system model is asynchronous, meaning it won't lock up your GUI while it's sitting there querying the underlying file system looking for things. So if you're using the Q-Dur model and you're finding that your user interface is locking up whenever they click on something, switch over to the Q file system model. All right, now I've already got a user interface done and it's a very simple one. It's just a tree view on the left and a list view on the right. In the premises, we're going to load the folders on the left and the files on the right. A very simple little explorer application here. All right, I've got some includes already in here, the Qt Core and Qt GUI. And let's just dive right in here. So we'll say Q file system model and we'll say Dur model and Q file system model. We'll call this file model. Now you might be asking yourself, why am I making two models here? We can attach multiple views to the same model. They're both going to be on the same directory. Why two? Well, we need to filter these. We want the Dur model to only display folders and we want the file model to only display files. Let's see what I mean here in a second. So let's go into dialog CPP and first let's set up the tree view. So let's say Dur model equal new, Q file system model. And what that does is it creates our new model. And what we need to do now is we need to actually populate this. So let's get a root path going here. Q string and we'll say S path. And what we want to do here is just get a path. So we'll say I'm going to use my f drive as the root path here. Now the reason why you need a root path is because the Q file system model will not do anything until it has a root path. So once you say S path, it actually goes out to the file system and says, OK, give me everything in that path and that's how it populates the model. And we'll say UI, tree view, set model. And this is where we actually set the model to the view, attach those two together. Now if we save and run this really, really quick, I say really quick, of course it will take a second to build. Your build times may vary. My computer is kind of going slow as I'm doing a virus scan. You can see I've got all these hard drives and I've set the root to f. And you can see how it's showing folders and files. Well, we want to get rid of that. We only want to display folders. So let's go in here and what we need to do is we need to set the filter. Whoops, there we go. We do that by saying qdir, no dot and dot, and qdir, I should say or qdir. And we want all dirs. What that'll do is only display the directories and hide the dot and dot folders. So let's save and run that again. Just kind of show you how that's working. Now when we go in, notice it's not displaying files anymore. It's only displaying folders. What we need to do now is we need to go to slot on the tree view and do clicked. And notice how it's got a qmodel index as an argument. That gives us the qmodel index of the node in the tree view clicked. Let's leave that for now. What we're going to do is we're going to actually implement the list view now. So we'll say file system model or I should say file model equal new q file system model. And pretty much the same thing. We just need to set the filters. And we can actually just do a little copy and paste magic here. But instead of directories, what we want to do are files. So our list view is only going to display files. That's why you need two different models because you got two different filters set on them. All right. Now we can say ui, list view, set model, file model, and that will set up our list view. Now when the user clicks on a node in the tree view, we want to be able to a, extract the path out of that tree view, and b, set that path into our list view. So q string, s path, wow, I cannot spell that, I'm sorry about that. And what we need to do now is get the path out of that dir model. And so we can just say file info. And notice how it takes a qmodel index. So you just plug the index in there. And then we want the absolute file path. So we're just saying dir model, file info for the index, and then we're going to say absolute file path. And that will give us the full path that the user just clicked on. Now we say ui, list view, and we want to set the root index. What that does is it sets the index for the root. So it's already got the underlying file system, and we're saying, okay, find this specific thing. And we'll say file model, set root path, and we give it the path that we extracted out of the tree view. So we're doing a couple things here. Let's review really, really quick. Scroll up to the top. All right. We are setting up our dir model and setting up the tree view. Then we're setting up our file model and then setting up the list view. Then when the user clicks on something in the tree view, we're extracting the path out of the node using the file info and absolute file path. And then we're saying list view, set root index, and we want the index of the set root path. Save and run. And wait for this thing to build. I really should turn virus scan off when I'm doing these. I apologize. There we go. Now when you click on something, you can see how it only displays files. Over here is displaying folders. Let's actually go into source code. And let's just see. And you notice how the icons are extracted from the underlying file system. So very quickly, we have just made an explorer style application. I realize this is very rudimentary, but something like this in other languages could take you just hours and hours and hours of coding. But using the model view architecture with Qt, it's actually very simple, especially when you use the Qt file system model. Now I should note you can accomplish roughly the same thing using the Qdure model. But the Q file system model is asynchronous and will not lock up your GUI. So this is Brian. Thank you for watching. I hope you found this video educational and entertaining.