 Hey everybody, this is Brian and welcome to the 21st Qt Tutorial with C++ and GUI programming. Today we're going to discuss something I've gotten quite a few emails about. The QTree widget. What is a tree? You see right here where it says GUI. All these items, this is a tree. It has items, each item can have sub items. And you can expand and collapse those items. That's a tree. We're going to actually build one of those. Now before we do, unfortunately the tree widget is a different animal than what you're used to. So it's going to take a little bit of programming to get there. So just follow me along if you can. Let's just say we got Qt Core and Qt GUI. We're going to add that into dialog H, that way we have those references. And we're going to make a couple function prototypes here. So we'll say void, add root. Q string name. And we just want the name. And let's say Q string description. And then we want to void, add child. And we're going to do pretty much the same thing. But we're also going to add a QTreeWidgetItem as the parent. So once you got those two function prototypes in, go ahead and take a second to type all that out. I know it's kind of choppy, but it's going to make our life in programming land a little easier. Just copy those. Go down to your dialog here. And I like to copy and paste, but you can actually type out. And the Qt IntelliSense is actually very strong in this category. And it will definitely help you out. But like I said, I'm really limited for time on YouTube. So let's just do this. Let's actually compile this, make sure everything runs the way we expect it so far. I know we don't have a very massive program yet. I just want to make sure there's no bugs creeping up and everything runs. So good. Let's go to our dialog. And let's add a push button, our epic click me button that we're used to. And remember the model based and the item based. We're not going to mess around with the model based yet. The model based, if you watch the last tutorial, you have an underlying model and you modify the model and the view updates. Well, we're not there yet. So let's take the tree widget, see how it's item based. And let's just drag and drop one on the dialog here. Resize it around a little bit. And then let's set the layout. That way we have something that we expect. Flip back to our code. All right. So now we've got this add root and add child. What are in the world are we going to do with these? Well, let's just add root here. And we'll say hello and world. And I should probably back up real quick here. I'm sorry about that. Let's add a tree widget and we'll say set column count to two. Now in case you're wondering what set column count does, you can have multiple columns in this tree view. Sounds kind of funky, I know. But it's doable and we're going to do it. So let's go down into our add root. And we will say queue tree widget item. Make a pointer to it. And we've got some properties here. In the parent, we're just going to say UI tree widget. And then we can start modifying this item directly. So we're going to say set text. Now you notice how it's asking for a column and a queue string. Well, a column is the position it's going to go in. So just follow me along for now. And we will say column zero is going to be name. And let's just do the magic of copy and paste here. Say one. And that's going to be the description. So we're just going to have two columns name and description. And then we're going to say UI tree widget. Oops. Add top level item. And we want to add our item. And that's really it. Now those of you that are familiar with C++ are going to wait a minute. All the foam. You're actually creating something here. How come you're not deleting it anywhere? Well, once you set the parent, the parent is actually responsible for that. Notice how we, in the constructor, set the UI tree widget parent. So when the tree widget's disposed, all the children of that tree widget are going to go bye-bye. So you don't have to worry about pointers. Compile and run. Make sure this thing doesn't explode on us. Oops. Expected semicolon before token. Yes. We made a very, very beginner error. So compile and run that again. And there is our top level item. Hello world. This is what I mean by columns. You can actually resize these out. So it looks kind of like a list. But you can add sub items in here. And that's where our add child function comes in. Pretty similar. But instead of adding it to the tree widget, we're going to say parent. Add child. And we're going to add the item there. Save that. Run it. Now where is it? Pesky little guy. Well, we haven't called it yet. So let's go add child. And let's add a couple of these really. Let's say item. And let's say to compile and run. And if everything goes well, there's our items. Now, why are they top level items? We said add them in. Well, if you go back into your code here, strip out this parent. Oops. Compile and run it again. Now you see how that works. The reason why I did that was simple. I wanted to explain that in a tree view, the parent is very important. If you create this item and you have the parent being the tree view, it actually adds the top level item. So in case you're wondering, yes, you can actually get rid of that. Compile and run it. Same thing. So that is the tree view. And we can actually add a couple of these. So let's say add a few of them in here. And let's just throw some numbers in there so we know which one's which. And there are our items. Sounds pretty complex, but it's really simple once you get the hang of it. And the tree view is a very, very powerful control. There are tons of applications out there that use this. And the great thing about Qt is this is cross-platform. So this code that you just wrote, you can take this on a Windows, a Linux, a Unix, a Mac, a Solaris. Anything that the Qt framework is supported on, recompile it, and it'll run virtually without any changes whatsoever. I mean, if you do some OS-specific items, then you'll have to change those. But for the most part, this cross-platform, actually in many respects it's better than Java and cross-platform, but I know that'll spark a whole religious debate. We won't go there. So I'm out of time. This is Brian. Thank you for watching. I hope you found this video educational and entertaining. And stay tuned.