 Hey everybody, it's Brian and this is our fourth cute programming tutorial with C++. We're going to go over signals and slots a little more in depth because you really need to understand this concept to really work with cute programming. So go File, New Project, GUI, and we're just going to call this My Progress. Give it a path and just leave the defaults for now and no version control. Here's your framework version and there is our beautiful window. What we're going to do is we're going to drag a progress bar and a horizontal slider. And you just right-click on the toolbar and go Remove Toolbar and you can do the same thing for slider for now. Maybe if I could find it here, move status bar, there we go. And then let's just resize this. One handy thing you can do is you can actually select your objects, right-click and go to Layout or you can use these snazzy buttons up here and you can select different types of layouts. We'll get into layouts in other tutorials, but for this one just do a vertical layout. Alright, now we've gone over signals and slots before. You click the Edit Signal Slot button and then you choose the control that has the signal, in this case the slider. Click and drag to the control that has the slot that you want. When you do that, a window appears and you can choose the signal and the slot. The signal in this case is the horizontal slider's value changed. And notice it has an argument of an integer. What that does is it says when the value of that slider is changed, you know when the user moves it, we're going to emit the signal of value changed. The slot that's going to consume that signal, we're going to say set value for the progress bar. That's how you actually set the progress bar's value. It's at 24% right now but you get the picture. Hit OK and that's how you do it with the drag and drop method. Just run this and wait for it to build and compile. And notice how it's still at 24% even though this is at zero because the value hasn't changed. But once you move it, voila, you have a fully functioning program right there. Now that's fine and dandy but sometimes you want to actually write the code and that's what we're going to cover in this tutorial. And by the way, just to delete a signal slot, you just select the signal slot path and delete it. Just press the delete button on the keyboard. But for this tutorial, we're going to flip over into some code here. Main window, go to the main window constructor and I'm going to actually hide the sidebar here. Now with QObject, the QObject macro that we've discussed in previous tutorials, there is another macro called connect. And what connect does, if you might have guessed, connects to objects via the signal slot mechanism. And you can see there's some arguments we have to provide, the sender, the signal, the receiver and the slot. So let's do that. Let's say ui.horizontalslider and the signal. And let's just choose value changed int and I'm going to break this into two lines just so you can see what's going on. Now the receiver for this is going to be the progress bar and it's a slot. And we're going to set value. So let's just break this up a little bit. The connect function is calling a sender with a signal, a receiver with a slot. And that is how you connect those programmatically. So let's actually compile and run this. And sure enough, does the same thing. That's how you do it via code. Now another interesting thing is there's always the opposite. If you've worked with .NET or Java, you understand this. But if this is new to you, bear with me. Disconnect, disconnects that signal slot mechanism, makes it so it no longer exists. You can connect multiple signals to multiple slots. So let's say you wanted to connect this signal of the horizontal slider to multiple progress bars. You can actually do that. Actually, let's go ahead and do that. That's a good topic of discussion here. Show sidebar. Let's drag the here and let's actually go here. Progress bar. So now we have two progress bars. We have progress bar and progress bar underscore two. That's just the default naming convention. You see the object name there. Let's go back into here. And let's actually just, through the magic of copy and paste, anybody remember the name of that control? I think it was underscore two. We'll find out here in a minute. Comment that out. Save our work. Let's build and run. And sure enough, as you can see, we have one signal being emitted, this horizontal slider. And it's being consumed by two slots, these two progress bars. It's pretty interesting how you can do that. Now let's say we wanted to just disconnect that first one. Remember, this is progress bar. Take a moment. Look at this code. What do you think is going to happen? Well, if you guess that progress bar two will work and progress bar will not, you guess correctly. Let's actually run this and you can verify that that works. So that is signals and slots in their very basic sense. I'd love to say they get much more difficult and much more complex, but really they don't. That's all you need to know. 99% of Qt programming is based on the signal slot mechanism as far as objects communicating with each other, including multi-threaded objects talking to each other. So it's a very, very good idea to get very good with signals and slots. I encourage you to follow this tutorial and practice and try your own things. All right. This is Brian. Thank you for watching. I hope you found this video educational and entertaining.