Hi. I'm Jesper Pedersen from KDAB. I'm afraid I have to make a statement.
I think my good friend (not that I ever met him, but his danger at least), Bjarne Stroustrup, forgot one thing. When he created C++, he had private (that's your internal stuff), protected (that's your subclass), public (that is your interface to the rest of the world), and he forgot the fourth thing: namely, how you communicate to the rest of the world. Go and look up in your design pattern book.
You'll find there are lots of design patterns for how objects communicate with each other. Qt fixed that problem back in 1994, using a mechanism called signals and slots. Until then, let's not go there.
Until then, there were a lot of void pointers. In java, there was a lot of sub-classing from an observer and an observee and whatnot. With Qt, we got a much cleaner way of doing it.
That's what we're going to see in this video and the following three videos. So, please join me on a tour back to 1994 and a half, or whatever it's going to be. I'm going to show you a small animation on how a slider and a spin box can communicate with each other.
It goes like this: So I have a slider over here. I got a spin box over there, and they want to communicate. The user takes the slider and presses something on it, and what happens when he does that is the slider emits the signal just like this.
I'm the slider now, and I'll go like this: MY VALUE HAS CHANGED TO 42! Does anybody care? Well, the slider doesn't care if anybody cares.
It just announces to the world its value has changed to 42. And we are still back in 1994 or 5-ish now. So, please bear with me a bit for continuing the story.
What the spin box needs to do to listen to that signal being emitted from the slider is that it needs to implement a slot. Back in those days, we would write a section: private slots, protected slots, or public slots. And then we would list in that section all of those slots that could be connected from a signal to a slot.
Now, thanks to some technology that is not entirely unlike what your phone system in 1800 and something was like, we have the possibility of setting up a direct connection -- a copper wire from the signal to the slot. And that is our signal slot connection. Okay, let's fast forward to today, because the slots are not really needed in private slots, protected slots, or public slots as they were back then.
Today, a slot is just a regular method and we'll see that in more detail in the next video when we talk about how you implement your own slots. But the signal slot connection in code looks like this: it's a static method on QObject. The static method takes four fingers.
It takes who is emitting the signal, which signal is being emitted, who is implementing the slot or the method that we connect to, and which method we are connecting to. There are a few different variants for how you do your signal slot connections. There is the signal slot connection where my second and fourth parameters are pointed to member functions.
And then there's the version where the second and fourth parameters are just regular strings. There's even a version where I don't have four parameters, I only have three, and the third parameter is a lambda function. We're gonna talk about those variants in the next two videos.
But for now, it just looks something like this: you have your QObject on connect, your static method on QObject. Here is who's emitting the signal. Which signal are we talking about?
We're talking about the value chain signal. There could be many other signals out of the slider. It could be the user pressed here rather than the values being changed or the user was doing something else to the slider.
You can go to the QSlider documentation to see the complete set of signals that a specific QSlider would emit. Then, the third parameter that is implementing our slot -- so that's a pointer to this spin box over here. And, finally, which slot are we talking about?
Just as for the the signals, we could have multiple slots. And what you'll see in the real world applications -- not just Qt Widget applications, but also QML applications -- is that you will have lots of different slots. As I said, these days the slots are not special methods anymore but you could still mark them up.
But, in any case, you'll find that there's a lot of different things connecting to many different places inside of your object. So, to omit the signal somehow, I need to know that my value has changed. And what value was it that was changed?
Well, somehow I was notified about the new value and when that new value came around. I will write emit, e-m-i-t, in just one word -- very unfortunate type setting here. Actually, the word emit is just a hash mark defining emit to be nothing, but that's just between you and me.
Don't use that. Do write emit whenever you need it. So emit valueChanged, that's the name of signal, and then the actual parameters coming out -- so 42.
or whatever. And over in the slot. As I said, the slot is just a regular C++ method.
Yeah, I'm on purpose shutting up while you look at this diagram And then, I'm just waiting for your reaction. This is where you will go, "Wow! That's amazing!
" See, the amazing part, I'm sure. So, I'm from Denmark. Lego is from Denmark There's Carlsberg beer, but let's not go down that road.
In Denmark, all kids played with Lego and I've asked around the world where I've done training and lots of you have been playing with Lego too. Now, here is a blue Lego brick. Here is a red Lego brick.
I put the red brick on top of the blue brick. I hope you've got a good imagination because I forgot my Legos at home. So, here you can now see a red brick on top of a blue brick.
To me, at least, it looks entirely like a horse. Well, it's missing the legs, but whatever. It's a horse and I can play with it like a horse.
I had two things that didn't know anything about each other. I put them together and all of a sudden they were useful in a complete new way. It was a horse that I could play with the rest of the day.
See, that's exactly what we have here with the signal slots. I have this object over here: QSlider and I urge you to go to the documentation and look for a QSpinBox anywhere in the source code of QSlider. Please do write in the comment below if you find it.
Over here in the QSpinBox, again, there is no comment or no reference to QSlider. So, I have these two subclasses of QObjects that don't know anything about each other, and we put them together to work in unexpected ways. At least, when I met signals and slots for the first time, I found that to be very interesting.
A few words on signals and slots, how they are supposed to be working: You're supposed to take a signal and use it as a notification mechanism. Remember, "MY VALUES CHANGE TO 42. " You shouldn't be listening to what the other side answers back.
It's not communication; it's a notification mechanism. You can connect a signal to zero slots and it's just me standing here. So, does the tree make sound when it falls in the forest and there's nobody around?
Let's not go down that road either. See, that's the signal being emitted. You can connect a signal to multiple slots.
So, you have multiple parties that are interested in listening to that signal. You can connect multiple signals to a single slot. You can even throw away information that the signals bring out.
Hey, my values change to 42. I don't care what your value is. All I need to know is that your value changed, because now I'll do something because your value changed.
That's what we see here in this table. I can take my range change that brings out two integers and of course my signal can bring out more than just a single value. So, I could bring out two insecure or whatever parameter I want, and I can put it into setRange and setValue and update.
So, I throw away one of them. I throw away both of the parameters. I can take this one, the value change that brings out a single integer, and put it into setValue, into update, but I cannot put it into setRange because what would the second one be?
I would need to invent that. If I really want to do that, hey, that's where lambda expressions enter the scene -- very useful. We'll see that in two videos from now.
And, finally, I cannot take my clicked and put that into setValue for the same reason. And then there's this one here. I'm gonna take my textChanged with QString and put that into a setValue with just a single parameter that is an integer, because I cannot convert its QString to an integer without added magic between those two classes.
And, as I said, I can connect a single signal to many slots -- many signals to the same slot and one signal to another signal. That is likely the feature in Qt that is used the least. At least, in my many years of programming with Qt, I very, very seldom connected a signal to another signal, but you can do that and that's cool.
That's it for this video here. We now understand the basics of how to connect signals and slots. What we're going to see in the next two videos is the three different ways of connecting signals and slots.
We're going to talk about the macro-based and the pointer to member function ways of connecting. And then, in the video following that, we're going to talk about connecting a signal to a lambda expression. And then, in the video following that, we'll see how we create our own signals and our own slots.
And that's when the picture is going to be complete and you're going to be living in lots of happiness after that. But until that day when you will learn other happiness from knowing how to create your own signals and slots, have a great time. I'll see you there.