 Hey everybody, this is Brian. Welcome to the 90 second Qt tutorial with C++ and GUI programming. Today we're going to be going over what's called Q settings. And also what we'll just call as my settings. Put it in the usual location. And I have to apologize for not making a video in a while. I've actually been very busy with work and the kid and everything else. And I took VMware training, so I'm now VMware certified. Had to take a few weeks off for that. Alright, so let's grab a couple push buttons and throw them out on our form here. And of course let's just align those. Now Q settings, in case you kind of guessed, allows us to save and load settings. And do it in a platform independent manner. And if you hear my cat in the background howling, it's because, well, she misses me. I haven't actually been in my office doing anything for a while. So she's, whoops, so she's kind of wanting my attention here. So we're just going to make the slots for the clicked for these. So here's save. Here's load. Now let's actually jump out here. Q settings class, if you go up to the help and documentation says, provides persistent platform independent application settings. What does that mean? Well, whenever you make an application, you want to include some settings and then have those settings persist from each time you run the application. For example, let's just run our little window here. We have a size and a position. Let's say we want to move this and resize it and set it over here. And we want it to load right here every single time we start this application. Well, how would you do that? Well, if you're in Windows, obviously you throw in the registry. Well, that's nice, but Qt is cross platform. So we want to do this in a cross platform manner. And that's what Q settings allows you to do. It's very simple, very easy to use. It's got some very powerful features that do to time constraints we won't really get into. So I encourage you to go out to the help system and look up Q setting. All right, let's go here. First thing we want to do, of course, is add some includes. And of course we're going to include Q settings. Some of you out there are going to go, now Brian Q settings is probably in Qt debug. Well, you're probably right. But for the sake of argument, this is what we're going to do. So now we want to make two functions. Save settings, which as you might have guessed, save settings and load settings. Very simple, very easy. Now go back out to our implementation here. And let's just go here. And what we're going to do next is we're going to take the save settings. So when they click the save button, it calls save settings. And same with load settings, we're going to put that in the load button. Now some of you out there are probably saying, well, why wouldn't you just implement everything in the button? Well, it's called design. Basically, we want to implement it once and call it many times. So we don't want to call on push button click because that makes no sense. We want to call load and save because it makes more sense. For example, we want to, when the window is first shown, call load settings. So now we can call this multiple times. All right. Go to Q settings. Some of you may realize from my typing that I'm kind of getting used to Visual Studio. I've been working in Visual Studio a lot more than Qt Creator. Mostly because I've been working in .NET and not Qt. I know, I'm sorry. And let's just call this my app. And let's call this my setting. So now that we have created a Q setting, and what we need to do is we need to give it a name and a subname. If you look in the documentation, it'll tell you the kind of prefers that you call it like your company. It's like organization and then application. So you could just say my company, my app. Be careful doing this though. You don't want to say like Microsoft and then Office because you may inadvertently overwrite some settings that you need. My cat is really being whiny. Go away kitty. Daddy's recording. That's right. Go away. Of course now that I've paid attention to her, she's going to want to talk to me even more. And then we're going to begin a group. And then now that we've begun a group, we want to end the group. Oops, not end array, end group. Now we're going to really get into groups. That's kind of something for more advanced tutorial, but just for the sake of brevity, use groups. In case you're wondering really what a group does, it allows you to group your settings into a logical unit. For example, you could say main window and then you could have them grouped per window. So like main window, sub window, about window, things like that. Now for this tutorial, let's say we want to save the geometry of this window. So we're going to say settings, set value, and we're just going to say position, and we're going to set the geometry of this window. And then just so we can see something actually happened here. Let's just throw a queued bug out there for good measure. And that is how you save your settings. Now to load your settings, it's actually very similar. We can pretty much just copy and paste this code out. And we want to make a queue rect. Call it myRect. Yeah, I know some of you out there are probably snickering at my name. That's the best I could come up with it in short notice. Call setting, value, and then what we want to do is actually get the value that we previously stored. Remember the name position? There we go. And we want to convert this to a rect. Let me get rid of this here. There we go. Now you can see all the code. So really all we're saying is, okay, same thing, company, application, group, and then we're going to get the variable that we stored. Now we want to do something with this. So we'll say setGeometry. And we're just going to set it to the value that we pulled out. And that's really all there is to it, other than we want to say loaded instead of saved. So let's run this. All right, we've got a good build. Should be popping up here any second. Now if you follow the tutorial thus far, you're wondering where your window is. You see how it says loaded? It's running, but there's no window. That's because we never saved anything initially. So when you ran your application, it said loadSettings. So it loaded nothing. So it had a 0, 0, 0. So it was just gone. Now let's run this again. And there's your window. That's a pitfall you should really be aware of. And there is a way to add defaults here in your value. Right here. You can give it a default value. Like a default rect. You know, like give it a 0, 0, 100 by 100. But for our example, we're just going to negate the whole default. Now let's run this again. We haven't actually done anything. We haven't saved anything yet. So let's kind of throw it down here. Click save. You see how it says saved over here? Meaning we have saved those settings. So now let's just move it over here. Give it like a funky shape and really move it over. And with quick load, magically, it loads the settings and sets the geometry of the window. Now when we comment out loads, or uncomment load settings, run this. It'll pull those settings from, you know, we're on windows. So it'll pull from registry and load it exactly the way it was. Pretty neat. Some things you should be aware of. Like I've already demonstrated. If you have not saved the settings, you should definitely use a default, which we did not do. Because if you don't use a default, what'll end up happening is you'll have some screwy results and you may unexpectedly crash your program. The program may just not appear like you just saw. Some other things you should be aware of is your naming convention. The reason why they recommend company application, et cetera, et cetera, is because you don't want to inadvertently overwrite some other applications settings. For example, let's just say we set the Internet Explorer homepage to 12345. Well, that's not a valid web address, so it's going to just, you know, not do anything special when it loads. But it could be more catastrophic. What if we set a critical registry key or an INF entry to something that was actually needed for that application to run? Well, then you end up reinstalling the application. So, that's all for this tutorial. Thank you for watching. I hope you found this educational and entertaining. And once again, I apologize for taking so long and now I've just been really busy. Been trying to keep up on the feedback. I am going to be putting all the videos into playlists. That's something that I've gotten probably about, oh, close to 200 requests for. So, that'll be kind of my main focus for a while. Anyways, this is Brian. Thanks for watching.