 Everybody it's Brian and this is our 31st Qt tutorial with C++ and GUI programming We're gonna tie everything we've learned together into a working GUI application. So let's go file new and we're going to say projects GUI application and call it My GUI thread let's just say E-Test and Let's give it a Q dialogue. I'm gonna have to go through this fairly quickly A lot of this is going to be review here. So just if you see me whipping through this and you have no idea what I'm doing I beg you, please go watch the other tutorials And we're just gonna throw a couple push buttons on here call this start and we'll call this stop And let's actually set our layout for this form here And let's put a spacer in here and the basic premise here help effect spell number The basic premise of this application is when we run it We're gonna have a thread that increments the number right here, and we're gonna have the ability to start and stop the thread So let's do that now. Let's jump right into the code Add new you guessed it C++ class And we'll say my thread Base class is going to be Q thread in the past. I've told you not to do that But I'm actually gonna tell you to do it this time The reason why is you get a lot of things for free and Here they are you automatically get the inherited Q thread with the Q object you get these signals and slots So in order for this class to talk with our GUI, we need to emit a signal. I Don't think we've ever done a signal, but they're very easy. You just need the Signals directive and then you just say void and let's call this number changed And that's it. That's how all it takes to make a signal and we're going to make the run Function override that and we're gonna make a boolean value called stop That's what we're gonna use to stop our thread Now let's jump into the thread implementation here and say void run Oops, totally forgot our namespace there. Let's just copy it for sake of speed And let's do an include here and you guessed it We are going to just simply do a for loop and I Going to make a mutex here That way we can avoid any embarrassing crashes Nothing's really changed. So if This stop this should all look very familiar to you break Otherwise we are going to do what is called an emit Emit is how a signal is emitted or sent from the object that other objects can put into a slot or absorb so we're going to admit the You guessed it number changed now in itself number change doesn't do us a whole lot of good because we need to take this I This variable and emit it through that slot. So let's actually jump in here and add a Parameter or an argument of integer So we're going to emit number changed with an integer Jump back into our thread here and now we can just put I Now if there are no slots to absorb the signal No problem. Nothing's gonna happen. Nothing's gonna crash signal will still get emitted, but nothing happens Now right now let's go into our dialogue here and this is where the magic is gonna happen Let's go include. Let me fix that real quick. Sorry about that My thread and we are going to make my thread Gonna make a pointer and we want to Actually make a public Slots on number changed. We're gonna give it an int That way we can absorb that signal as it's emitted Now jump into dialogue CPP and you notice how it's just a bare bones basic dialogue here So the first thing we need to do void dialogue We need to implement our slot Because that is after a little point of this program. So let's say you I label Set text all of this so far should be very familiar and nothing but pure review Q string If you're having problems keeping up with what I'm doing. I'd recommend you go back and watch some of the other tutorials Because we have covered all of this so far All right, so that is how we are displaying The number let's kind of review real quick here as the thread is run It's going to loop 10,000 times and it's going to emit this number changed and we are Using a slot to absorb that signal and when it's Triggered we are just simply putting that number out there Now we haven't run this thread yet. So we need to do that. Let's actually go into our dialogue Start go to slot clicked Let's go back in here and do the stop button also go to slot clicked That way we have on push button on pushing to button to let's say started Comments are always nice. I know I haven't been doing them very much in this tutorial So we have started and stopped Now first thing we need to do is actually create the thread. So I'll say m thread equal new My thread And we're going to make this the parent that way when this forms close the threads also delete it out of memory And we want to connect that signal and slots. So we'll sit connect m thread signal You guessed it number changed with an int these slots going to be this I'm sorry. The receiver is going to be this the slot However is going to be On number changed That's the slot. We just created with our integer parameter and then We will jump down when the started button is clicked We're going to start the thread with the normal priority and when the stop buttons click we're going to actually Set that stop flag Remember we put that q mutex in there so that if it's in the middle of that loop reading that variable and we're setting the flag It's not going to explode and blow up in a fiery death here. Oh Forbids on number change with no type. What did we do wrong here? Oh, yes, we forgot void very sorry about that so Go ahead and build your program and Fingers crossed there we go when we Click the start button notice how it counted up not counted so fast. We didn't have a chance to even test the stop button Well, that's what I was talking about in a previous tutorial when I said we had to use sleep This and we could say sleep Which you have an unsigned long and I believe that's every second but we want to do milliseconds So we'll say MS sleep or millisecond sleep and let's give it a number like um, I don't know 100 milliseconds Nice even number. We can still see what's going on here Now we click start you see how it's counting and it's counting fairly quickly and you can click stop and the thread stops That in a nutshell is how you make a thread with GUI programming Now you notice if you're familiar with other languages like visual basic C sharp and I believe Java We didn't have to do any proxy or delegate classes That's a real pain in the butt with some of these other programming languages Is you have to make a separate class to interface with your thread class to interface with the GUI? Cute doesn't have any of that nonsense whatsoever. You just use the standard signal slot mechanism, which you've grown to love and learn Alright, this is Brian. I'm running out of time. I hope you found this video educational and entertaining and thank you for watching