 Hey everybody, it's Brian and welcome to the 108th cute tutorial with C++ and GUI programming. If my voice starts cracking, I have to apologize, everybody here is coming down with colds and I really feel like I'm coming down with one too. Be sure to visit my website, www.voidrealms.com, and hold on my cat, this cat I swear every video, every video cat, go do something else, www.voidrealms.com, you can get tutorials. All that I do, I try to put the video plus the source code out here. Now I did have it on a host that crashed, so if by chance you're following along and you come across one of these tutorials where, well, the source code just isn't out there, do me a favor, zip it up and email it to me and I'll review it, and if it's legit I'll throw it out on my website for everybody else to download too. Most of them do have the source though, but there are a couple where the files are just gone and I haven't had a chance to go back through and look, but you can sort, search, filter, whatever you want to do. By the way, this website's written in Yee, a new tutorial that we've started doing with the Yee framework, you see we've got video one out there. Also got some programs I've written, some source code that's out there, free of charge of course, for example, a full FTP server I wrote in C++ with Qt, it's got some bugs in it, I haven't had a chance to iron it out, some projects I'm working on, if you're so inclined you can donate but please don't feel obligated, the site's 100% run by your donations. And how to contact me, easiest way is by email, I get a massive amount of email, I've had to turn my spam filter on, so if I don't get back to you either or it got gobbled up by the spam filter or you're just, I haven't gotten to you yet, I'm sorry. Alright so, I thought we had talked about this at one point but I guess we didn't. Marcus wrote in and said, Brian, I need a better way to work with the command line. I know these tutorials are about Qt and GUI, etc., etc., but command line, help me out. So we're going to talk about the Qtxtream and we're going to make a pretty beefy little command line, basically we're going to take command line and throw it on steroids here, so let me get rid of my cat here, come on kitty, go away, I love you if you go. Maybe it's just because I had Arby's a few minutes ago, but alright so we're going to actually create a new project and we're going to create a console application. Now why have we been focusing on console apps? Console, geez, console apps, been focusing on them because well it's a lot easier than mucking around with the GUI. We are going to get back into GUI programming, don't worry about that, I'm just letting you know that hey, you know, sometimes you gotta stick with the basics here. So this is a basic command line application. When we run this, not a whole lot happens. On some operating systems you can see yourself type on others you can't. We want something that works consistently across all operating systems and I know some of you out there are like, well you can just do the, you know, using, hope if I could spell it using, namespace, wow I really cannot spell the day, that's embarrassing, STD and then do your standard C and C out. You can do that but it's not very object oriented and it can be kind of cumbersome to work with especially when you get into larger projects. So we're going to add new, gonna choose class and we're just gonna call this command line and I swear I did something very similar to this, maybe I didn't but we're gonna inherit Q object and then we're gonna finish and get our header here. Now we're gonna include a few things so let's include Q thread. I do know that I wrote this a while back and I wrote it because, well the command line was really just making me mad, it just, it wasn't consistent, it wasn't working great on all operating systems and let's see here we're gonna include Q text stream. Like I said, I have to apologize if I'm clearing my throat or anything like that. It's just like the creeping plague has invaded this house and everybody's getting sick. So hopefully I got, hopefully I'll last through this video. Come on kitty here, you're really being a drag today. This cat's 20 years old so she's getting kind of clingy and I think it's, I think that time's approaching if you know what I mean, which I'll be very sad. I love this cat. Alright so we're gonna make our signal on read line and then we're gonna make a few slots here and we're gonna say void read STDN so we're gonna read the standard input and then we're gonna void handle the standard input Q string line, oops and let's make the private section Q thread M thread. So we're gonna make this multi-threaded and you'll see why in a little bit. So just a review, we've got our Q object we're inheriting, we've got our Q thread, our trusty Q debug, Q text stream and then we have a signal of on read line and two slots. Read STDN and handle STDN and of course we're making a thread. So we're gonna jump into the actual implementation right after we do this. Told you I was getting a little loopy, so it's cold meds. Alright so now we've got our beautiful code file here that we can start fleshing out. First thing we're gonna do is we're gonna say this move to thread and we're gonna give it a reference to M thread. So what are we doing and why are we doing this? Well we're taking this object as current class and we're going to move it to a thread. The cute documented correct way of handling threads is you'll create a thread, create an object and then move that object to the thread and then you will start the thread. So this class will be instantly multi-threaded. This class will run inside of its own independent thread. That can be very handy for when you get into looping situations and we'll talk about that here in just a second. First thing we need to do is connect our signals and slots. So we're gonna take our thread, we're gonna take the signal of, you guessed it, started. So when this thread starts we're gonna say this slot and we're going to read the standard input. So as soon as this thread starts, this guy right here, we're gonna read the standard input down here. So we're gonna start just, you know, this loop of reading the standard input. Then we're gonna say connect and we're gonna say this. Come on kitty, seriously. This signal, this cat loves me to death. I think Arby's is the only thing keeping it alive and we're gonna say slot and of course you guessed it, handle standard input. So what we're doing here is we're creating the object or I should say once the object's created we're moving it to a thread, connecting our signals and slots and we're starting that thread. This is where we're gonna actually read the input from the standard input. Boy that was a mouthful. Q-text stream. Anybody out there watch Breaking Bad? It's a TV show in America. Oh my gosh, I just watched the finale for it. I'm a little behind the times and I'm just blown away. That show is just awesome. Now we're gonna read the standard in. STDN is a variable in Qt which refers to the standard input. So what we're doing is we're making a Q-text stream and we're creating it with a reference to I should say a copy of the standard input variable. And then we're going to just make a Q string called line and we're gonna say you guessed it, stream read line. And then we're gonna emit our signal on read line with the line variable. And then we're going to just loop back into our software. So what this little function here is doing is it's saying, all right, as soon as this function starts, which is when the thread starts, we're going to create a stream with the standard input. We're gonna read line and then once we have a line of text, we're gonna admit our signal, I'm sorry, emit our signal. And then we're just gonna loop back into ourselves. So this is just a loop over and over and over and over and over. You could easily do this within a do loop, but I like doing this. It just seems a little cleaner to me. Sometimes I've had issues with programs where I do things like this in a do loop where they'll actually just lock up and I cannot for life me figure out what's going on. So to me, this just seems like a little bit easier that I can add more functionality, but you could definitely do a do loop. I know I'm gonna get some hate mail saying, why didn't you do that in the do loop? It's the correct way of doing it. So that's why. Now we're gonna do our handle. Now remember, we've subscribed to this. Our handle input is waiting for the on read lied signal. So as soon as this is admitted, it's going to go into here. So we're gonna just say, Q to bug. I accidentally hit caps lock Q to bug and we're just gonna say the line. I'm getting all sorts of interruptions. My phone's going. Some of my friends want me to jump into World of Warcraft and start playing with them. So we're just gonna echo this back out here. Now go back into our main. We're gonna say include command line. And we're just gonna say command line CLI. So what we've done here is we've created an instance of this. As soon as the program starts up, it's gonna take itself and move it to a thread, connect signal slots, start the thread. And it's gonna sit here and loop reading the line. This is why you do it on a thread. This guy right here, read line. This is another loop. Inside of this little function, read line, it's just looping and it's waiting and it's reading that standard input. Think of it as like a little gear turning. It's just gonna sit there and wait and wait and wait until the user presses the enter button. So if you don't do this whole thing in a thread, your program's gonna get to this and stop until somebody hits the enter button. So if you're processing a file or reading sockets or doing something else, it won't work. Your program's gonna just stop. So you take this whole class and move it to its own thread as soon as it starts. That way your other classes can do whatever they want. We have our signals so that we can have other classes that we create, subscribe to it. And we're just using this handle standard and just to verify that our signal slot architecture here is working. It'll run just fine without that handle standard in. So if we'll just say test, it'll echo it back, test. So how would you actually write to the standard output? Let's see here. Let's say void write std out. Let's call this queue string line. So now we're gonna write to the standard output. Well, you know me. I'm a copy and paste junkie. So we're just gonna do this. We're gonna change it from standard in to standard out. And then we can just treat it like C out. So to test this, we'll just say, okay, you entered line. And we can get rid of queue debug. Some of you have been wondering why I use queue debug. Well, queue debug's very helpful when you're debugging your application. You see down here debug. A lot of times when you switch to release, your queue debug messages go away. And you can actually set, I believe you can set different levels of queue debug messages that go out, something we might talk about in future tutorials here. So there's your write standard output, which you could, because we made this a slot, you can call this from other classes too. Save all. And if everything goes good, it doesn't explode. And we'll say, this is a test. You entered, this is a test. So just to recap for this tutorial, excuse me, my throat's really dry. We've handled standard in, standard out, how to read and write without using the C in, C out that you see in a million books, you know, the typical hello world application. We're moving it to a thread so that we're not stuck waiting for a line input like most applications are. This will because we haven't really made any pointers. We don't have to really worry about memory management. It's nice and neat. And it's all using the cute architecture. Any questions, comments, concerns? All right, I wish you were here to ask them. Anyways, this is Brian. That's all for this tutorial. My throat's getting really dry, so I'm going to go take some cough medicine and I hope to see and hear from you soon.