 Hey everybody, this is Brian and welcome to our 60-second cute tutorial with C++ and GUI programming I've gotten some feedback and it was hey where you been we haven't seen a tutorial in a while Well, make a long story short. I have been working. I'm a dad so that takes up some time My poor kitty which you've heard in the background of some of my videos has has been rather sick I've had to take him to the vet and he needs a Operation that's going to cost about six hundred dollars, which of course does not make me happy, but I love the little guy And I have been studying like mad for my certified ethical hacker exam, which is right around the corner So that's where I've been Now without further ado go ahead and just create a simple Simple console application as you can see we've just got our little bare bones app Let's just compile and run this just to show you there's no hidden voodoo magic going on here I had quite a few people contact me and say Brian Love your videos, but we did not really understand signals and slots. Could you go over those a little bit more? So we're gonna go over those If you know signals and slots inside now, please feel free to skip this video Otherwise, well, let's do it We're just gonna add new and we're gonna add a class and let's just call this person And we are going to say Q object is the base class And finish Now we're gonna keep this person class fairly simple We want to include And we're going to say Q string And we want to include Q debug That's a lot that'll just allows us to print things out here Now you see our class is very very basic It inherits Q object and it has this Q object macro What do these do? Well, we inherit Q object that way it has all the fundamentals of Q object and Q object macro allows it to work with the mock or the meta object compiler now admittedly I Might have the two flipped around and if I do I'm sure I'm gonna get many many many messages saying hey You're wrong. I welcome those messages Now what is mock? You've heard me mention that one's before MOC mock stands for meta object compiler What it does is it takes your signals and slots, which we'll be creating here in a second and Connects them it generates the code that makes all this stuff work So there's a lot of code in the background that gets generated that you never have to touch You never have to see because mock creates it and does all the magic for you Pretty neat, huh? So we're gonna make a signal. We're gonna say void Speak Q string And we're just gonna say words and now we need a slot so we'll say void Probably something my girlfriend wishes I do more often listen Q string in words So there's our signals and slots now a signal is something that happens We emit a signal meaning we tell the world. Hey, this happened a Slot on the other hand is when something happens. We want to consume that event Meaning the world is gonna tell us. Hey, something happened and we're gonna process that That being said one of these we have to implement and one of them we don't Well, if you guessed we have to implement a signal. I'm sorry you're wrong We don't need to implement a signal Because mock takes care of all that for us We do have to implement the slot because something happened and we need to process it So let's just go ahead and really silly example. I know but we're just going to create a little gossip here so we're gonna say void Person And I want to say listen and we're just going to Q debug and we're just gonna say someone told me And we're just gonna say Words oops There we go So when they listen, they're just gonna turn right around and say well someone told me and they're just gonna repeat the same thing They just heard that's pretty typical of people. I think I'm just kidding alright So what we probably need to do next is give it a little more information here because we want to get some sort of dialogue going at a water cooler So let's just say Q string Same name Now I'm forgoing the use of getters and setters We've discussed those in previous tutorials and we're just gonna throw the raw variable out there Ideally, that's not unique, but I'm just gonna try and save time and focus on signals and slots here So we're gonna say name and we're going to just give it a function Save void and we'll call it gossip Q string Words and let's just save our work and now we need to implement this So let's go in here and go void person gossip and We're just gonna Little copy and paste magic here and let's just actually put the name out here Well, I'm thinking about it. Sorry. I'm kind of doing this on the fly. So bear with me here name says So it's just gonna print out the name someone told me and for gossip. We're just going to say name name says So very simple. Let's review. We've got our person class, which is a Q object has the Q object macro. It has a name as a function called gossip has a signal called speak and A slot called listen Now notice we haven't done anything with signals and slots yet What we need to do here is gossip So we're going to actually emit that signal And we're going to speak Oops There we go, and we want to speak words. We're gonna say words. There we go Now when you emit the signal you're telling the world. Hey, this happened and anybody who's connected to that is going to automatically Get it and I'm going to demonstrate that next. So what we're going to do here is we're going to make a water cooler class. I Know the typical water cooler at work where everybody talks So I'll say water cooler. We're just going to give it a Q object Next finish alright Now we're going to pretty much just leave this blank And what we're going to do is just right in the constructor of this guy We're going to start gossiping, but we need to include some stuff first. So say include Help if I spelled include right, so we're going to include our person class And we're just gonna say person And we'll say Mary Actually not Mary. Let's say Kathy. We got our chatty Kathy here and then we'll say person and we'll say Bob Boy, I really need to start wearing my glasses again if I just spelled babe. Anyways So what we got is we got Kathy and Bob standing around the water cooler here And we need to connect these two because Bob's listening to Kathy. So we're gonna do a say connect and This is how you connect two objects together you have a center and receiver and the center comes first So we're gonna say Kathy Is the center and the signal and you actually have to say signal it's not liking me today for some reason Bear with me one moment. Okay. I see what I did wrong here. We are actually supposed to use a pointer Sorry about that. It's been a very very long day at work see slide one of this tutorial So we're going to give it a reference to Kathy And then we have to actually say signal and then the signal that we're going to use which is speak So we're gonna say do something and then we have to connect it to the receiver Which is Bob poor Bob has to listen to Kathy chat all day long and then we have the slot And the slot is of course listen Let's go to the end here so you can see this whole thing. I apologize for the Screen real estate here. Let's actually hide the sidebar so we've got our water cooler and We got Kathy and we got Bob and we're just gonna connect these two together and you're gonna say Kathy Is going to speak Bob is going to listen That's just very simply how you connect these two together Now notice nothing's happened yet. We still haven't done anything So we're gonna say Kathy gossip And we're gonna say I heard Mark is bald I don't know. I'm not a gossip queen. So I'm having a really hard time coming up with gossip here I'm guessing that's what a chatty cat he would actually gossip about So let's just save and run. Let's just see if this whole monstrosity even compiles here And sure enough it compiles, but we of course I forgot to do anything. So apologize include And we'll say water cooler Actually, we want to do the single quotes Now if I've never discussed why you use a bracket or why you use a single why use the double quotes It's because if you're using a bracket it means you're using a library and if you're using quotes That means you're using one of your files. Let's say water cooler Cooler And there's our constructor there. So when we fire up this water cooler bunch of stuff's gonna happen Let's pile and run this and Blank says I heard Mark was bald blank said someone told me I heard Mark was both. Well, yes, we forgot to enter the names Let's fix that up real quick here. Like I said, I apologize. I'm just kind of doing this on the fly Let's fix this up here and then we'll discuss it Name equal Kathy and we could have very well done this in the constructor, but I want to make this very simple We'll call this chatty Kathy and then Bob will say Bob dot name Equal we'll say poor Bob alright Save run. Let's see this thing work. Alright, there we go Chatty Kathy says I heard Mark is bald poor Bob says someone told me I heard Mark is bald We do have a Kathy where I work but she's not a chatty Kathy or a gossip queen and I don't think we have a Bob or a Mark where I work. So this is all speculation. Nobody get upset Let's just kind of dive back into this. We're going to go right from the beginning When the program starts it's calling water cooler Making a new instance of water cooler Now when water cooler is created, we're creating an instance of Kathy an instance of Bob Now I should step back and say we're actually creating two instances of person. This is the same class using it two different ways Now we're setting Kathy dot name equal chatty Kathy Bob dot name equal poor Bob and then we're calling connect Connect is how you connect two objects together To allow them to use signals and slots Now if you try to use that in the main because main is not a queue object you will get an error message because Main is just C++ code. It knows nothing of the meta object compiler or signals or slots or anything like that That's why I created the water cooler class chatty Kathy says I heard mark is bald Now because we've connected these signals and slots the signal being speak which is called during gossip Bob listen Bob's going to consume that signal. So when when Kathy emits the speak signal any class Connected is going to also receive that and let's just do that and let's just say Sally and little copy and paste magic here because I'm running short on time And we'll say silly Sally Now let's run this And I'll show you that notice how Sally is not in the picture here because we haven't connected her Let's just simply copy this You know so you can just switch out the the instance name there Compile and run and we now have three people at the water cooler chatty Kathy says I heard mark was bald poor Bob says Someone told me blah blah blah silly Sally same thing. So you can see how our instances are connected So that one signal is emitted and multiple slots are consuming it That's how signals and slots work. It's a very powerful mechanism and there are other libraries and frameworks out there that do that But cute actually does it very very well. I'm very impressed with the way cute does it All right, now let's kind of jump into the nuts and bolts of the person class here You have got the the gossip function and you have got the speak signal and The listen slot the gossip function lives solely to emit the speak signal now in a real-world application You would just pretty much say hey do this Now you might be asking why didn't we do this in the water cooler class? Why didn't we just go in here and say Kathy Actually, let's just say emit Kathy Speak I mean doesn't this seem like it would be any easier thing to do Well, if you try to run that you're gonna be disappointed because it's gonna say it's protected It's part of that class. You can't modify it outside the clash came call it Now I'm sure there's workarounds out there, but you should not let other objects emit on your behalf Meaning you don't want to let somebody speak for Kathy Kathy wants to speak for herself That's why you have gossip When I say gossip I actually mean sorry our function which when you look at the function Exists pretty much solely to emit the signal because it's protected We can call it from inside our instance, but other instances that cannot call it for us Basically a very simple way of saying hey nobody else can speak or emit or signal on our behalf All right now our listen function is also slot That's right. I said our listen function is also slot slots or functions functions or slots They're the same thing the only difference is You have put the function prototype under the slots That way the meta object compiler the mock knows that that function can be connected to a signal Very simple very easy So this is Brian. I thank you for watching and listening to me rant about my my work and my parenthood and my poor kitty and Keep the feedback coming. I like hearing from you guys