 Hey everybody, it's Brian and welcome to the 48th Qt tutorial with C++ and GUI programming. Today we're going to be continuing our conversation about the model view architecture and how that really relates to your daily life as a programmer. We're going to cover another predefined model called the Qdurr model, so let's just call us durrmod, put in the usual location, next, and let's do a Q dialog, finish, alright now that we've got our dialog on here, let's just throw a couple push buttons on here, and if you have the official Qt book then yes this is pretty much straight out of the book changing a few things just for the sake of time, and we're going to throw a tree view on here. Now remember, whenever you see the word view that means it follows the model view architecture. Resize these appropriately, set your layouts, let's just throw a horizontal space in there that way our buttons look semi-normal, and we're going to make a directory and delete. Okay, now that you've got your layout done, pretty simple layout I know, let's actually flip into the code here, and we're just going to add our includes, whoops, having keyboard difficulties tonight, I apologize, and we've got that in there, we're going to just create a new Qdurr model, and let's just call it model, flip back into our form real quick here, and let's actually just create these signals and slots what we're thinking about it, I'm just kind of going through this very quickly, I always feel sort of rushed when I'm making these videos just because I run out of time so fast, alright. Now we're in our implementation file, the first thing we actually need to do is create and populate our model, and to do that we actually just need to create the models, we'll say model, go new, let's say this is the parent, and one thing you should note is we should make the model not read-only, if it's read-only then you cannot modify the underlying file system, now you may want it read-only, for example if you're making like a file open box or something, but in our case we actually want to work with the file system, and let's just do a UI-treeview-set model, I want to show you exactly what this looks like just so we can continue on, compile and run, this won't have very much functionality, but you'll get the drift of what's going on once this thing compiles and actually runs, okay. Here we have, I realize I have a lot of drives, this is my gaming slash development machine, we have a list of drives, and this is just a tree view, the underlying model is actually our file system, and you can see how it's kind of haphazard and nothing's sorted, and well we're gonna fix all that, so bear with me, but I just wanted to show you the rough idea of what we're trying to accomplish here, we'll be able to make directories and delete files, so let's close that. All right now, let's say we want to do the sorting first, so let's go model, set sorting, and we need to use the queue dir flag, so say queue dir, and we want dirs first, or directories first, we want to ignore the case because we really don't care, and we want to sort by name, so I'll say name, very simple, what we're saying is we want the directories to be listed first, you also notice we could list the directories last, we want to ignore the case and sort by name, then what we want to do here is, let's just get rid of that extra line there, that always kind of annoys me, we want to actually have this automatically select one of my drives, so we'll say queue, model index, and once you start working with model view architecture, you realize just how important that the queue model index really is, and we'll say equals model, and we want the index of, and we're gonna give it a path here. Now, what you could do is that, but one thing queue does is it automatically translates the forward slash, that's kind of like a universal, universal translator for the path notation, I would not for production applications recommend hard coding a path in there, but this is just a tutorial, so we can kind of get away with that, we'll say UI, tree view, and we want to expand, and what we're doing is we're getting the index of the F drive, and we're going to actually find it the tree view and expand it, and let's just work a little copy and paste magic here, and then we want to scroll to, so we want to scroll the view down to it, that way we don't have to go hunting for it, and we want to actually select it, and then we want to resize the column to the contents, and we just want the name or the first column. Alright, save your work, and let's actually run this, see this thing in action here, and wow, doesn't that look a little better? You notice how it's sorted by name, it ignores case, and the directors are first, it found and selected the F drive, and it scrolled to it, pretty neat. Now, one thing you should know is that you already have the ability to rename files, without even adding any extra programming in there, so you should be careful when you're working with this that you don't inadvertently delete files or rename files, this is actually working with your file system, unless you have it in read-only mode, which we don't. So, let's go in here, and what we want to do is be able to create directories. So, we'll say q-model-index-index-equal-UI-tree-view-current-index, and we want to make sure we have a valid index. We say if index is valid, and I should say if not index is valid, sorry about that, then return. So, if we don't have a valid index, then we just jump right out of there, and we'll see. Queue String, now we want to get the name of the directory. We want to use a queue input dialogue, and we want to get text, and this is the parent, and we'll say, let's just call this name, that's the title, and the label will say, enter a name, and one thing we want to check here, whoops, apparently we have a boo-boo, what is the problem here? We've got the parent, we've got the title, we've got the label, not quite sure, oh yes, silly me, sorry, long day at work, a couple of you out there probably checkling. So, we want to make sure we have a valid name. So, if the name is empty, then we just want to just jump right out of here, and if all goes good, we've got a valid name, we'll say model-mecter, and we want to actually give it the index, that's the parent, notice how we got the current index up here, and give it the name, and the name is right there, so let's actually run that, and let's see if we can create a directory here. So, we've got our F drive selected, and let's just make a new directory, and there it is, AAA. Okay, now for the finale of this tutorial, we're actually going to delete stuff, so let's save ourselves a bit of typing by just copying, pasting here, and one thing we want to do is say, if it's a directory, or if it's a file, we have to handle it a little bit differently, so if, and you can actually, from the model, get the file information from that index, and from that you can pretty much do whatever you want, I mean you can determine if it's a file, if it's a directory, get the base name, etc, etc, etc, and then you can use Q file, like example, if you wanted to rename it or something, but in this case we want to just say, is it a directory? If so, then it's enter, otherwise it's a file, and we have to handle those differently. So, to handle a directory, you'd say model, removedir, and you just give it the index, and for files you'd say model, and you just say remove, pretty simple. Let's review real quick before we run this, basically you're getting the index from the tree view current index, and if the index is not valid, then you just break out of here, then you say model, get the file information for that Q model index, if it's a directory, then do a removedir, if it's a file, then just do remove. So let's save and run this, so let's actually get rid of BBB, sure enough it's gone, let's get rid of AAA while we're there, just to clean up my hard drive a little bit. All right, and let's delete that clock right there, so there you go, that's the Q, what is this again? Qdir model, sorry about that, it's been a very long day at work. One thing you should note about the Qdir model is that it's actually been replaced, and what do I mean by that? Well, let's go into help, go Qdir model, and what it says is the Qdir model class provides data model for local file system. If you click more, it actually goes in and says the usage of this is not recommended anymore, use Q file system model instead. Well, what's the difference between Qdir model and Q file system model? Well, the biggest difference other than they don't recommend using Qdir model anymore is that the Q file system model is actually asynchronous, which means it won't lock up your GUI, but with everything it's got its own little complexities that you have to deal with. We will cover the Q file system model in another tutorial and try to discuss some of those complexities, but that's it for this tutorial. Thank you for watching, I hope you found this educational and entertaining.