C++ Qt 62 - Viewer Feedback Signals and Slots in depth

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody 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 his tutorial in a while well make a long story short I've 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 take him to the vet and he needs a operation that's going to cost about $600 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 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 going to go over those if you know signals and slots inside now please feel free to skip this video otherwise let's do it I'm just going to add new and we're going to 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 going to 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 this a lot that just allows us to print things out here now you see our class is very very basic it inherits Q object and as this Q object macro one of 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 going to get many many many messages saying hey you're wrong I welcome those messages now what is MOC you've heard me mention that once before MOC MOC stands for meta object compiler what it does is it takes your signals and slots which will 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 MOC creates it and does all the magic for you pretty neat so we're going to make a signal we're going to say void speak cue string and we're just going to say words and now we need a slot so we'll say void probably something my girlfriend wishes I do more often listen cue string any 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 going to tell us hey something happened and we're going to process that that being said one of these we have to implement and one of them we don't well if you guess 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 going to say void person and want to say listen and we're just going to cue debug and we're just going to say someone told me not the top and we're just going to say words oops there we go so when they listen they're just going to turn right around and say well someone told me and they're just going to repeat the same thing they just heard that's pretty typical to people I think I'm just kidding all right 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 cue string st. name now I'm forgoing the use of getters and setters we've discussed those in previous tutorials we're just going to throw the raw variable out there ideally that's not unique but I'm just going to try and save time and focus on signals and slots here so we're going to say name and we're going to just give it a function save void and we'll call it gossip cue 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 for some gossip and we're just going to little copy and paste magic here now 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 going to print 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 is a function called gossip has a signal called speak and a slot called listen now notice we haven't done anything with signals that slot 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 so we're going to 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 watercolor class I know the typical water cooler at work where everybody talks so let's say water-cooler we're just going to give it a cue object next finish all right now we're going to pretty much just leave this blank and what we're going to do is just write in the constructor of this guy we're going to start gossiping but we need to include some stuff first so include help if I spelled include right so we're going to include our person class and we're just going to say person and we'll say Mary actually not Mary let's say Kathy we got our Chatty Cathy here and then we'll say person and we'll say Bob go ahead really need to start wearing my glasses again if I just spelled babe anyways so what we guys we got Cathy and Bob standing around the water cooler here and we need to connect these two because Bob's listening to Cathy so we're going to do a sake connect and this is how you connect two objects together you have a sender and receiver and the sender comes first so we're going to say Cathy is the sender and the signal and you actually have to say signal mmm it's not liking me today for some reason hmm 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 1 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 going to 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 going to connect these two together and you're going to 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 going to say hmm 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 Cathy 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 apologized 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 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 stuffs going to happen let's compile and run this and blank says I heard mark was balled blanks that someone told me I heard mark was but 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 pick this up here and then we'll discuss it name equal Kathy we could have very well done this in the constructor but I want to make this very simple we'll call this Chatty Cathy and then Bob will say Bob name take one we'll say poor Bob all right save run let's see this thing work all right there we go Chatty Cathy says I heard mark is bald poor Bob says someone told me I heard Marcus bald we do have a Cathy where I work but she's not a Chatty Cathy 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 water color now when water cooler is created we're creating an instance of Cathy and instance the 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 Cathy name equal Chatty Cathy Bob 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 cue 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 clue class Chatty Cathy 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 Cathy 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 a little copy-and-paste magic here because I'm running short on time and will 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 just eye and just switch out the the instance name there compile and run and we now have three people at the watercooler Chatty Cathy 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 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 a the gossip function and you have got the speak signal and the listen slot the gossip function lives solely to admit 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 watercooler class why don't we just go in here and say Kathy actually let's just say admit Kathy speak I mean doesn't this seem like it would be an easier thing to do well if you try to run that you're going to be disappointed because it's going to say ain't it's protected it's part of that class you can't modify it outside the clash game call it now I'm sure there's workarounds out there but um 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 a slot that's right I said our listen function is also a 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
Info
Channel: VoidRealms
Views: 45,003
Rating: undefined out of 5
Keywords: Qt, 62, Viewer, Feedback, Signals, and, Slots, in, depth
Id: qEGRYYx0RBw
Channel Id: undefined
Length: 17min 33sec (1053 seconds)
Published: Mon Mar 14 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.