 Hey everybody, this is Brian, and welcome to, wow, what are we up to, 109. Cute tutorial, 109. Before we get into it, be sure to visit my website, voidrums.com. I've got all the tutorials, and most of the source code, you can help out if you're watching one of these tutorials and the source code is missing. Well, I was on a server, the server crashed, the company I was paying never took a backup, so some of the files are now missing. So if you're watching along and you're doing one of these tutorials, like let's say we're just doing any of these, and you notice the source is missing, go ahead, type it out, zip it up, and I'll review it, and if it looks legit, I'll post it up here for everybody else. All right, let's dive right in here, and we're going to say enums. The power of enums. What are enums? Well, enums are enumerations, so we're going to add a new class here. I'm just going to call this test, we're going to hear it QObject. We're going to break apart from the standard enum tutorial. I mean, everybody at this point should know what enumeration is. If you don't, well, go read a C++ book. Actually, I think I have a C++ tutorial on that. All right, so include, and we're going to include QMetaEnum. What in the world is that? Well, Qt's gone a long way to add metadata. What is metadata? Data is data about data. For example, a string. Well, how do you know what a Q string really is? Well, a Q string is a wrapper for blah, blah, blah. You understand that, but QMetaEnum actually allows that to happen. Adds a bit of, if you're a Java programmer, reflection or .NET programmers probably know that term too. So we're going to add a macro in here, and we're going to call it QEnums. Whoops. How if I actually spell it right? And we need to give it a parameter. So our parameter is actually going to be an enumeration. We're going to call it Tester, and we'll call it Tess. Now we need to actually make our enum. Now if you remember from all your little C++ classes that we've been taking over the years here, an enumeration is quite simply just a list of things. So we're going to say test1, actually let's do all caps, test1, test2, test3. So we've got three different enum items here. Now we need to actually make something happen. We'll call it doTest, QString, command. So we're just going to make a simple slot. We're going to define that. Now what's going to happen is someone somewhere out there is going to take our class and they're going to throw a string at us. We're going to actually switch on that string. That's right, the switch command. Now anybody watching this is going to go, whoa, hold the phone. You can't switch on a string in C++. Well, that's true to a degree. That's why the QMeta enum is out there. So what we're going to do first is we have to actually get that metadata. So we're going to say QMetaObject. And we're just going to call it MetaObject, real classy there. And we're going to say this static MetaObject. So what we're doing is we're getting the Meta information about this object. So we're getting information about it or data about data. Now we're going to get the MetaEnumEnumerator. And we're going to call this QMetaEnum. We're going to call it MetaEnum. And this is going to be the MetaObject. And we're going to get the Enumerator. So we're getting data about data and we're getting the Enumerator. And we're going to say MetaObject index of Enumerator. There it is. So we're getting the index of the Enumerator. And notice how it's taking a const care pointer to name. So it's getting a constant care. So what we're going to give it, of course, is, well, the name of our Enumerator, which was, if I remember right, we called it Tess. Let's jump back here. Yep, Tess. So what's going on? What are we really doing? This is all confusing. All right, let's jump back into the code here. We're getting the, let me end that correctly, we have our class. When the class is first loaded or compiled, I should say, we're building MetaData about it. We're saying QEnum, which is a macro. And we're calling ourEnum of Tess. So we're saying build MetaInformation about this Enum. This Enum has three items in it. Don't get hung up on this just yet. We have a slot. In that slot, we're saying get the MetaObject, this static MetaObject. So we're getting data about this object. And then we're getting the MetaEnumerator, meaning we're getting that Enumerator. Now, it seems all confusing, but I promise you, it's really not that bad once we get down to the nitty gritty of it. Really, all you need to understand at this point is that we're getting data about data and that we're going to get data about our index of Enumerator, which is Tess, our Enum object. Whew, that's a mouthful. If it sounds confusing and complex, it's because it is confusing and complex. But we're gonna make it simple. So, now all we wanna do is we wanna be able to switch. So let's actually add in our trusty old, oops, include Q to bug, and we're gonna actually switch on this. So we're gonna say, mm-hmm, mm-hmm, mm-hmm, you guessed it, MetaEnum, keys to value is what we want. And you notice how that returns an int. That's how we're able to switch this because C++ cannot switch on a QString or even a QMetaEnum. It needs an integer. So we're gonna actually convert that string into an index. So we're gonna do keys to value and we're gonna say command to Latin one. Why? Because it wants a, I haven't actually tested that. We probably could just pass a QString, but it wants a plain old boring C++ type string. So we're gonna say case and there's our enums. And we're gonna say Q to bug. Okay, kitty, I'm telling you, every tutorial this cat is right here. We'll say do something for test one. And we're gonna do our break. And we're just gonna do this for each one of these enum items. We had one, two, and three. And then it's always good to, when you're working with these, to do the default. And we're just gonna say unknown command. Whoopsie, I'm trying to type while my cat's nuzzling up against my arm. Let's fix these up a little bit. Two and two, three and three. Whew, all right. That's quite a lot of work that we're doing here. All right, let's go into here. Say include, we're gonna include our test class. Say test, say test, do test. And we're gonna give our QString, so we're gonna say QString. And we're just gonna give it invalid data. We're gonna compile this. And sure enough, we got our default unknown command. So we can tell the program's actually working here. Now we're gonna say test one. You can see once again, unknown command, because of course C++ is case sensitive. So if we actually do the uppercase, run this again, do something for test one. So now if you were writing a program and the user enters some input and you wanna switch on it, you know how to handle that. Now, a real easy way around this whole case sensitivity of course is just to go in here and say, oopsie, two upper, two Latin one. And then it simply won't matter. See, do something for test one, ta-da. So let's recap real quick because this can be kind of a confusing topic. First off, why do you need this? Well, it makes life a little simpler. People use enums for case, you know. You get in the car, do you drive to A, the store, B, the movie theater, C, somewhere else. So it makes life easier when you're doing conditional statements, AKA you're using a switch. Well, if you're working with strings, you cannot switch on a string. Therefore, you've got to use Q enums to gather the meta, or I should say create metadata about this enum. Then you can actually retrieve metadata from the static meta object and then get the index of the enumerator and then switch on that as it were an integer. Neat, huh? Well, that's all for this tutorial. Thank you for watching. I hope you found this educational and entertaining. I look forward to your feedback.