 Hey everybody, this is Brian and welcome to the 27th Qt tutorial with C++ and GUI programming. We're going to do something a little bit different. We're going to step away from the GUI portion and delve into a little more difficult subject matter because I think you guys are ready for it. So, fire open Qt creator and go file new and let's make a new project and we're going to make a console application. I'm going to stay away from the GUI portion just because I really want you to understand the concepts without the GUI really cluttering anything up. And we'll call this timer, oops call this my timer test. Put it wherever you normally put it and next, next finish. And what we're going to do is we're going to create what's called a Q timer. If you don't know what a timer is, think of a clock on the wall. That's a perfect example of a timer. It counts every second, the second hand moves every minute, the minute hand moves every hour, the hour hand moves. So it's actually three timers in one seconds, minutes, hours. You get the point. So what we're going to do is we're going to make a very simple timer. So what we need to do here is actually create a class. So I'll go add new and we're going to go Qt. I'm sorry. I'm going to go C++, C++ class. And we need to give it a name here. So we will say my timer. And we're not going to fill in base class or any of the silver information because I want to explain some stuff as we go. So just leave it as is and hit next and finish. And it creates my timer h and my timer dot cpp, which in themselves are pretty much blank. They just look like a standard C++ class. So let's fill in some includes here. Get that out of the way. Qt core. And then in the my timer dot cpp, we're going to do the same thing. Include Qt core. Sorry about that. I've got my keyboard set up for call of duty black ops. So it's doing crazy things as I type. All right. So let's say include, we'll just say Qt debug. Now let's flip back into the header here. We want to be able to use the signal slot mechanism in Qt. In order to do that, this class has to be a Qt object. So what you need to do is actually inherit the Qt object. And you could do QWidget or anything else, but we're just going to use the Qt object, which is the basic object in Qt. Now, there's one more step. And this is often a misstep and it will cause a lot of frustration. You have to add the Q object macro. What this does is it converts this class into a true Q object so it can actually use the signal slot mechanism. Without this, the mock or meta object compiler, which runs in the background every time you compile this will not work. If you're wondering what mock does, it actually connects the signals and slots that we've learned about connects them all together. But once you have the inherited part and the Q object macro, you're good to go. And I actually at this point recommend you just build it just to make sure it compiles. Because if it doesn't compile at this point, you're not going to get anything to run right. As you see, we had a successful build, so we can continue. All right. Now we're just going to say Q timer. And we'll call it timer. And then we're going to make a slot. Public slots. And it's just, if you're wondering, because you've never done this before, yes, a slot is just a normal function. The only difference is you have this public slots. That is the only difference right there. As long as your function is listed underneath a slot, you get a public and private. As long as it's listed underneath slots, it'll be treated as a slot. All right, now let's flip into our actual implementation file here. And we're going to say timer equal new Q timer. And we want to make the parent the current class. And the reason for this is the class is an object. And if we make an instance of the class, when it's deleted, it'll delete all the children. And we're making the timer a child of this. So the timer pointer will automatically get deleted. All right. Then we will say, let's actually implement our slot here. My timer. And we'll say my slot. You notice how this has a special icon as the slot icon. And there is your code for your slot. Voila. Not that hard. We're actually going to say Q debug timer executed. That way every time the timer is executed, it's just going to print out timer executed. Now, we're not done yet. We've created our timer, but we haven't told it to run. So what we need to do is actually connect it. And the signal is the timeout. And the receiver of that is this, the current object. And the slot is, you guessed it, my slot. Then we can actually take our timer and start it. Now, you notice how it has a couple different ways of doing this cut to constructors. It has just a default and in milliseconds. Well, if you do it in milliseconds, you can actually describe where I should say you can relay how many milliseconds you want in between. So we're going to do a thousand. That way every second or 1000 milliseconds, it's going to fire. And when that timer goes off in memory, it's going to call my slot. Because remember, we connected it right here. We're still not done. We need to actually create an instance of this. So let's go include my timer. And then let's actually create instance of this. And that's it. All right. Now, before I run this, I want to walk through the code really quickly. In the main.cpp, we are starting our application. Here's our Qt core application with arguments and return exact. And in between there, we're creating a new my timer class. The my timer class is a Q object. And you see we have the Q object macro and we're inheriting Q object. And we've made our own slot called my slot. And in the constructor, we are creating a new timer in memory, connecting the signal and slot and starting the timer. And it's going to go off every 1000 milliseconds. And every time it goes off, it's going to print out timer executed. So fingers crossed compile and run. And if you've followed along, and if I've typed this correctly, the application will start. And then you can see that the timer fires every 1000 milliseconds. Now, it's important to note that this is not a perfect 1000 milliseconds, it's off by a couple of milliseconds. So you may notice if you're doing some high performance application, you may need to find another route. But for the basic timing mechanism, this is perfect. So this is Brian. I hope you found this video educational and entertaining. And thank you for watching.