 Hey everybody, this is Brian and welcome to the 82nd Qt tutorial with C++ and GUI programming. We're just going to call this View2 and we're going to continue with the Q Graphics View. Put it in the usual location. Sorry about that. My throat is still really sore from being sick. I don't know what the deal is. Finished a full course of antibiotics and I'm still sick. Not sure what's going on with that but can't be good. Alright, let's find our Graphics View and throw that on there. Save that and now what we're going to do is we're going to actually create a custom object. Let's say we want to throw something on the scene but we want custom behavior. We want to throw a square on there and when we click the square it changes color but we can still move it around. Obviously to do that we need to, you guessed it, create our own item. So, without further ado, let's just jump right in here. Let's go add new. We're going to make class and let's just call this my square. Next, finish, finish. Alright, let's move this over here so we can see what we're doing. Grab the header for this and we're going to add a few includes here. We're going to include the Q Graphics item and we're going to just include Q Debug here. May or may not use it. I'm not sure yet to be honest with you. If we run into any major hurdles here we'll definitely use it. Alright now, first thing we want to do is we want to inherit. So we're going to say public because we want to inherit all the public parts of, you guessed it, Q Graphics item. Now the shapes that we put into the previous tutorial, the rectangle and the square, those actually inherit Q Graphics item. So we're actually just creating our own custom little object here. Going to say Q Rect F. Q Rect F, it's a floating rectangle. Basically you can use floating numbers. And we want Bounding Rect. It's going to be a const. If you're wondering where I'm getting all this from, it's directly out of the Q documentation. You can just look at Q Graphics item and there you go. I mean you got all the information you need. Pretty hefty description on how to make your own items but we're just going to cover the basics here. Now you need the Bounding Rect because the scene asks the object, hey what is your Bounding Rectangle? Now Bounding Rectangle is the outermost edges of the object. So even if you have some really funky weird looking object, you still have an outer rectangle or a Bounding Rect. And we're just going to grab this paint function here. And I already have all the little details filled in here. Let me grab these for my notes. I'm trying to save some time. I know these videos get kind of lengthy. I've had a few complaints that they get a little too long. So it's just going to fill in the variable names here just for the sake of argument. So now you notice how the paint gives us a pointer to a Q painter. Gives us a Q style option graphics item. Those are just options and the parent widget. Now those are important because, well, you need to be able to paint this thing. Now we are of course going to override the mouse stuff. I say the mouse stuff. We want to know when the mouse is pressed and we want to know when the mouse is released. So let's throw that back up here. Now we have to implement these. Let's just go ahead and jump in here and refactor these. I'm just going to add these in here really, really quick. And if you're wondering how I'm getting there is just you right click on the function name, go to refactor and then add definition and it just adds a code right in there. I found that little trick and I'm addicted to it. Now we need to add a little something in here so we know the state of our object. Object state usually becomes pretty important. So we're going to say pressed and we just want to know what's going on with this object here. So my square in the constructor, we're just going to say pressed equals false because, you know, we're not pressing it. Now we have our mouse events here. Now when the mouse is pressed, that's when the button is clicked. So we're going to say pressed equals true. We want to call update which forces this object to paint. You notice how you can give it a balance but we're just going to repaint the entire object. And then of course we want cue graphics. Cue graphics item and we want to just throw this right back up the food chain here. We're going to say mouse press event which is copy and paste all this in here and say false. And mouse release of course is when they take your finger off the mouse button. So now we know when the mouse is pressed and released. We need to tell it the bounding rectangles and we're not going to get too fancy with this. I just really want to focus on how to implement this. So cue rect and let's just say 0 0 100 100. And remember that's the bounding rectangle or the outermost edge of our shape. No matter what our shape looks like, it's the outermost edges. And then we have to implement paint. What we're going to do here is we're just going to get a cue rect and we'll just call it rec. And we're just going to get that out of our bounding rect function. So we're just returning the 0 0 100 100. And then we're going to make a brush so we can say cue brush. Maybe if I can spell it. And we're just going to real simple here cue blue. Now we want to do a little bit of logic here. We want to say if pressed. Now remember we get pressed based off the mouse state. So if it's pressed we're going to do one thing otherwise we're going to do another thing. Remember when the mouse is pressed we're setting pressed to true. When it's released press is going to false. So we're saying if it's pressed the mouse is being held down. We want to say brush. Silly me. Set color and let's say we want to do cute. We want red. So when that mouse is down we want it to be red. And when it's released we want it to be blue. Actually let's say green. Now we get our painter object and we're just going to say fill rect. Give it the rect object and give it the brush. Painter object here and then we'll say painter draw rect. And we'll just give it your rect object. We're just going to use the default pen for this tutorial. So real quick review of our class here. Jump back into the header file. We are inheriting the cue graphics item. And because of that it says right in the documentation you must implement bounding rect. Which returns a cue rect floating. And you must implement paint. We added custom boolean variable pressed. And we are overriding the mouse pressed event and mouse release event. Just so we know the state of that object. And then of course here's our object itself. And just very simply we're setting the balance to zero zero one hundred one hundred. We're just going to paint it red if it's pressed green if it's not. A very very simple object here. Alright now. Let's create open dialog.h here. Now in dialog.h of course we want to get a reference to, you guessed it, our object. So we'll say include. And first we've got to do cue core. Just going to kind of plow through this here. Of course as soon as I start speeding up my typing gets horrendous. Oops where did I get that. Let's do cue gooey. Sorry about that it's been kind of a long day here. Just got off the phone with a head hunter before I started making this video. It's a very hard time explaining to this young man that I do not want to leave my full-time job for some temporary position. It's just not happening. And we want of course cue graphics scene because we want to make a scene here. One of the few times in life you actually do want to make a scene. I know someone out there right now is probably snickering going Brian. Don't quit your day job. Comedy is not your strong suit. I'm going to say my square and we're just going to call this square. So now we've got our graphic scene and our square. What we need to do next is just very simply implement this. And this is pretty much review from what you've been doing. So I'm just going to kind of fly through this here. Get our cue graphics scene and UI, graphics view. Say set scene. So now we have our scene object. Now for the interesting part of this tutorial. What we need to do now is create our object and add it to the scene. So we're going to say square. Kind of botched that up a little bit didn't I? Now I know that somebody's going to write in and say Brian you're leaking memory right there. Yes, you're right. I am. I'm sorry. I'm going to do it just once for this tutorial. And we want to add item and notice how it's a cue graphics item. So any cue graphics item you make you can now just suddenly add to your scene. One thing we really didn't do yet. Let's find it. Is we didn't tell this object that it is movable. So I guess if you really really want to get fancy with this we could just say set flag and then you could set your flags right here. Item is movable. That way we can just add this to our application or sorry add it to the scene. And then we can move it around inside the application. Once this thing builds and runs. There we go. So there is our item. When we click it it turns red. Release it it turns green. And we can click and move it around the screen. And as you can imagine you can get pretty extravagant with some of these items. I mean it's totally you know whatever you want to do. Let's just say painter draw ellipse. And let's just give it the rect. So we're just going to draw an ellipse inside of our square here. Now it looks like we have two objects but it's actually all one object here. So you can get as complex as you want as long as it stays within the bounding wrecks here. Anyways I'm running out of time. This is Brian. Thank you for watching. I hope you found this tutorial educational entertaining. And keep up the feedback. I like hearing from you guys.