 Hey everybody, this is Brian and welcome to the 96th cute tutorial. Wow, we're closing in on that 100 mark. Today we're going to do something a little different by finishing up XML. We're going to make a GUI application. I was over inundated with email from people saying, look, buddy, we want those real world examples. We really like those. So we're going to say XML editor. I had literally 300 emails saying, Brian, please make an XML editor tie everything all together. So here we go. I'm going to make this a Q dialog. And I have to apologize to those of you out there that have been messaging me and I still haven't gotten your messages. I've got a backlog of about two and a half months worth of messages. It's getting kind of ridiculous. I'm actually getting more messages than I have time to keep up with. I may need to ask somebody to go through them for me or something. So we're going to slap a tree view on here because we are going to use the model view framework and a push button. And we're going to give you just a basic scenario here. The scenario is very simple. Your boss comes in and says, hey, I love the work you've been doing with this XML thing, but you know what? I really don't understand XML. I want something simple and easy to use. So make me a GUI. We're going under that premise. That's my story and I'm sticking to it. So this is going to be a very simple dialog here. We're just going to have a tree view with a button that says Save. Save this and run this. Give this a good build. Give you an idea of what our window is going to look like here. So very simple. The dialog will automatically load the file, which we'll cover that here in a minute. This is going to be somewhat of a longer tutorial. So my cats unfortunately hate longer tutorials and beg for my attention. So you may see me pause the video a few times. Anyways, and then when we click the Save button, it'll overwrite the XML file. So without further ado, let's just dive right on into the code here. All right. Now, first thing we want to do here in dialog H is we want to actually add some includes. And for the sake of time, I'm just going to copy and paste from my notes here. I don't like doing a lot of copy and pasting because I feel it kind of cheats the purpose, but for something like that, you really just don't need to sit here and watch me do that. All right. XML. Remember to add XML into your project file or you're going to get all sorts of funky weird errors. Like that was underlined. You see it? No. I like to, after I do something like that, give it a good build just to make sure everything is working. And while it's building, I can tell you my woes. I have this chipmunk living under my back deck. I have this deck in my backyard. It's nothing fancy, but man, it seems like every summer something's living under there. All right. What we're going to do next is we want to add some things. For example, we want to add a Q standard item model. And I'm not going to get too in depth into the model view architecture because we've already covered that. So if you're kind of wondering how to work with the model view framework, I would suggest you watch my previous videos on it. Little shameless advertising there. I've also been getting some very good feedback. Like I said, my time's been just insane right now. So I've been having a real hard time getting to you guys' messages, but I love the feedback that I've been actually able to get to. And please don't get discouraged if I'm still trying to get to yours. I know it's frustrating when you send somebody a message and they don't get back to you forever. And unfortunately, I'm turning into that guy. So we're just going to add a couple of variables here. Our model, our file name, a read function, and a write function. So let's just go ahead and factor head definition. I love those functions. All right. And let's go ahead and go into our dialog here and let's go to slot, clicked. That way we know when they click the button to save the document. Now as you've guessed, we've got some things we need to do here. This is our very bare bones, what we need to do. When the dialog loads, first thing we need to do is read the file. So we've got to do a little bit of setup first here. Go away kitty. Daddy's recording. I swear this cat. I love her to death, but she leaves me alone. I'm invisible to her until I start recording. Then she's just all over me. I think it's you guys. I think she just likes to say hi. Okay. Let's see here. Kitty. You're not helping. I have to pet the cat. Sorry, guys. So file name equal and I'm just going to copy and paste my file name here. Actually, let me pause at this cat's getting on my nerves. All right. Sorry about that. I can promise you over this tutorial you're going to hear me say sorry about that for pausing this video at least a dozen times. All right. Now if you're wondering where I'm getting this XML file from, if you're just tuning in, it's the XML file that we've been working with through these tutorials. And some of you have given me flak about using Internet Explorer and you know what? Internet Explorer is awesome. Now, actually, I use a couple of different browsers. I just use Internet Explorer for these tutorials because it's, well, to be bluntly honest, it's just the closest icon to my mouse. But to get this XML file, it will be included with the source code on my website. Or you can just use it from the previous tutorials that we've talked about. All right. Now we've got to create our model here. And I'm going to say new queue standard item model. And when we do this, we've got to give it some constructors here. And if you're wondering what this voodoo magic that I'm typing in here, I implore you to click queue standard item model and press F1 and then read up on the constructors. Like I said, I'm not going to go over it in this tutorial because we're really focused on the how to make an XML editor. And yes, we did cover this, I believe, in our previous tutorials on the item view. If not, somebody correct me and I will promptly say sorry. All right, now we've got our read file function, which basically we're just going to read the files contents. And then we are going just to say UI, TreeView, SetModel. If you know the model framework, this is pretty simple stuff for you here. But quick review before we get to in-depth. We're just setting the file name. And this is the pre-generated XML file that we've been working with. We're making a new queue standard item model. We're setting the constructors. And then we're calling read file, which we're going to populate that model. And then we're saying the TreeView, SetModel, and we're setting the data model. So now we get to the fun part, or I should say lack thereof. Sometimes reading and writing files aren't very fun. But we'll try to spice it up, maybe throwing like a random clown or something. Queue, yeah, can't type. Queue standard item is what we're looking for here. As soon as my cat, let's go with my sleeve. Kitty. I'm actually becoming known on the internet as the guy with the cats. It's actually kind of funny I'm becoming the cat lady. Queue standard item. Let me get rid of that real quick. There we go. Give us a little more screen real estate here. Now we are making our root element here. And the reason why we're doing this is because if you remember from our tutorial on XML, you need a root element. Well, the thing is we're making an XML editor. We don't want them to be able to edit the root element. The only thing we want in our hypothetical situation of the boss wants something is we want him to be able to edit the name. And that's it. We don't want him to edit the ID or the book. So we've got to kind of protect some of these things. And some of you have also commented on my bookmarks, Blackjack and Rift. Yes, I play Rift. I'm trying cleric and I've actually started playing Blackjack at the local casino. I'm not very good at it though. I've lost some money. Don't worry, none of your donations have gone towards the casino. And to be brutally honest, I haven't spent very much money at all because I suck at Blackjack. But anyways, enough of that. And we're going to append row. We're going to add our root here. Actually, I should jump back up here. Say constructor, we're saying zero rows, one column. Sorry, I probably should have covered that. So now you understand why we're doing the append row. But seriously, I encourage you to go watch the Model View video that I've done or read up on it. Now we're going to make our queue DOM document. And now we're going to load the XML file. Queue file, we'll call this file, file name. And then we're going to just say if file open. And here's the fun part. QIO device, you're remembering some of this stuff. Remember, you can press Control, Space to bring up the intelligence. So we get QIO device, hit Tab, colon, colon. And then you just do read only. This is one of those tutorials admittedly. I kind of wish that I would copy and paste a little bit. But I've tried that in different things for different tutorials that I have not published on YouTube. And people just didn't like it because it just seemed like it just jumped right over the topic. You didn't really understand what you're doing or why. And when you type it out, it kind of forces you to learn by doing. Now we're going to set the contents of the file. Or I'm sorry, set the contents of the document from the file. So we're basically just going to give it a reference to the file. And we're going to let it read it in. And if you have no idea what I'm doing here with the set content or queue DOM document, please, once again, I have to reiterate, go watch the previous tutorials because this is building upon what we've already learned. I know some people don't like that. They like to just jump right in the middle. But I'm sorry, this is kind of an advanced tutorial. Now we're going to get the XML root. And we do that by getting the queue DOM element. And we'll call this XML root. And we'll do this document first child element. That way we don't have to convert it. We just get the element. Now we want to read the books. So this is where the code's going to get kind of mucky here. So we want queue DOM, nodes list, and we'll call this books. And we want elements by name from our XML elements. And let's just say book. And so this isn't so much some voodoo magic here. If we flip into our XML file, you see there's our root books. And what we're doing is we're saying get element by name. And we're saying book. So every element that starts in book, we're going to get. So we're going to get this element. Remember elements can contain elements. In this case, we've got books that contain chapters. Right now, we're just getting a list of book elements. Go back into our code here. Uh-oh, hold on. Kitty needs attention. Go away, kitty. I don't know what it is. This cat is just tormenting me lately. So now we just want to go through the nodes list here. Wow, my fingers are really, really sore. I've been doing a lot of housework. I think I said that in a previous tutorial. And ripping wood siding off your house is not fun. And my hands are like really, really sore right now. I've been done for a while, but wow. Now we want to get the book. And what we're doing here is we're getting a specific index. And what we're saying is we want the book element at. And we'll say to element. And in previous tutorials, we've kind of broken this into a couple lines of code. But we're just going to squish it down into one line of code. Because we've got a lot of code to write today. And then we want a qStandardItem. And the reason why we want this is because we want to actually add this into our model. So we're going to say bookItem equal new. I can't type today. qStandardItem. Now we've created a new qStandardItem, which we're going to insert in our model. So now we need to pull the data out of the XML file and put it into the item. And we do that by saying bookAttribute. And we want to get, let's flip back into our XML file here. What attribute do we want to get here? We want to get name. Because that's what we want our boss to be able to edit here. It's a fairly large line of code there. And it's worth noting that we are creating a new pointer here. Some people will try to omit that and then wonder why they get errors later. So take special note to that that's what we are doing. Now that we've got our item here, we want to read the chapters of the book. Pretty similar. What we're going to do, a little judicious copy and paste here, we want to get a qDOMnodeList. And we'll call this chapters. And we want to do, whoops, because we've got our book item now. We want to go in here. I'm going to pause this real quick. Sorry about that. Once again, my cats, my little carpet sharks, as I call them, circling for attention, whoops, for int h equal 0. I did have a good question the other day. One that made me think a little bit. I like questions that make me think. Somebody asked the new standard of C++. If you didn't know, there's a new standard out called C++ 11. Or I think it's 2 or something like that, I don't remember. Anyways, C++ is a very, very slow evolving language. There's been very few changes to it since its inception. What we're using now is actually C++ 86, I think it is, because the new draft was in 1986. Well, C++ 11 just came out. Or I think it's 2. Correct me if I'm wrong. I think it's a binary joke thing that I honestly don't understand. And there's a lot of new features. And people were like, well, are we going to be using those features? Well, the answer to that is soon. Not right now, but soon. The reason not right now is simple. It's not currently compatible with the compilers we're using. I'm sure some of the compilers out there, like I've heard the Microsoft compiler, is actually really up to speed on that. But like most things, Microsoft, they've put their own little spin on it. And that really just grinds my gears. This is a good expression, a good child-friendly expression. All right, so now we're making a chapter item. And you're probably wondering why we're making the book item in a chapter item, because we're going to add both of these into, you guessed it, our model. And we're getting our attribute name, because once again, let's pull up our XML file. The chapter has an attribute called name. And that's what we want our boss to really be able to mess around with. Now that we've got those, what we want to do is we want to actually add that in. So let's just scroll over here. And we're going to say, whoops, book item. And we want to append row. And we want to append the chapter item. What we're effectively doing here, whoops, I went to hit Control-S to save it. What we're effectively doing here is building a tree model. We've got our book item, which we're going to add to our model. And we're appending the chapter item in here. Now what we need to do is break out of this little loop here and say, root append row. And then we want the book item. Whew, that was a lot of typing. So let's actually give it a good compile and run and see what happens here. We want to make sure this actually builds and runs at this point, and we don't have any real major issues. And if we do, I'll pause the video and figure out what the issues are. Tick tock, tick tock. Maybe I should pause the video while it's building. Now there it goes. All right, so here is our, ta-da, our application. And you can see, you can expand it out now and you got my book, blah, blah, blah, blah. Now we have a bug here. Hmm, can anybody figure out what the bug is? Books, my book, my book. It should be books, my book, my chapter. Let's flip into the XML file here and see what's going on. Yeah, see, it should be my chapter. So let's actually kill that, kill that, and let's figure out what's going on here. Hmm, let me pause the video. Ah, yes, went a little too fast. You can see this right here on line 53, chapter equal books at, no, we don't want that. We want chapter equal chapters at. That way we're getting it from the right collection here. Let's build and run this again and I'll bet you a dollar because now I'm a gambling man with Blackjack that it'll actually work. And there we go, there's our chapters here. Now, if you're wondering why we did the Model V framework is because it gets built in editing right into it. The only thing we need to do now at this point is take that modify document that's in memory, click the save button, which will write it out to disk. Pretty simple, pretty neat. So let's jump down here and you could argue we don't even need the right file function because we could just do it right in this on push button clicked. It's personal preference. I personally like building things out. I call it plumbing code because let's say you got like 16 things that you need to call the same function here, then I would just have one function called 16 times rather than 16 versions of the function. That's just how I do it. But with Qt you have actions. So you could have done an action called on right file clicked or whatever. Just doing real simple, keeping it simple for illustrative purposes. Now we're going to write the XML file. And what we're going to do, write the file file, write the XML file. What we're going to do here is we're going to basically just recreate this document from scratch and overwrite the existing. It's a lot easier than loading it, moving it around, trying to figure out what's what and editing it in place. We've already got it edited in memory. So we'll say Q DOM document. And while I'm writing some comments here, keep up on that feedback. I know I'm horrible about getting to it, but I do kind of scan through mic at root known not mic make a root note. Unfortunately, I can't multitask to save my life. But what I'm looking for is ideas for the 100th video. The problem I have is, well, time. Unfortunately, I love to help people, but I get a lot of people that want help. And I've been getting a lot of people saying, hey, I'm a student, I'm in school, my test is due tomorrow. Can you help me? And unfortunately I don't even get to that until about three weeks after their test. So I hope you did good on that test. Create element. Now what we're gonna do here is create our root element. The reason why you don't let your boss modify that root element is because, well, it'll throw your whole document off and then you won't have anything to grab on to. All right, so now we've got our root element. We need to do a document. And we'll see, what else has been going on? Oh yeah, I've got a blackberry. Actually, it's kind of funny. People have been sending me viruses trying to infect me. They see me using like Windows with IE and Outlook. So they send me links to things that would infect my computer and it's actually kind of funny because I read my email on a totally separate device that is, I don't want to say immune to viruses, but if it got a virus, I would just literally reload the thing in five minutes. So, Q standard item. So if you're one of those people that's been trying to infect me, I implore you, keep trying. It's a good learning experience, but you are horrendously wasting your time because I don't read email on this computer. I just won't do it because, well, I don't want to get infected. All right. And I also jumped between Windows and Linux. I've actually been in Linux a lot lately, which is another question. Sorry, I've been kind of goofing around today, but another question that somebody had was, why don't you record these in Linux? Cutest cross platform? Well, I don't own a Mac and I think they're a bit pricey and I do have Linux. I love Linux, but I cannot find a good video editor for Linux. I just, I try just about everything out there and each one has some sort of horrible bug where it either won't record the video's choppy, something's wrong. The last one I tried, I just, I couldn't get the sound to work to save my life. I'm wearing Windows, so I hate to say it, it just works. All right, now, quick review of what we're doing and then I'll get back on my phone conversation here is we're just going to write the XML document. We've already got the document in memory. So we're going to just rebuild a new document. We're going to say QDOM XML root. There's our root element, we're pending the root. And then we're going to get the model item at zero zero or the root model, the root of that tree view. And then we're just going to go right on down and say, okay, for each item in the root, we are going to grab each item here. Q standard item. But anyways, I've got a Blackberry and if you have been watching the news, they've been a Blackberry blackout. All of Europe's has lost Blackberry functionality for like three days and it hit North America today and I'm in North America. So it's like my phone is just worse than normal. I've been wanting to get a new phone for a while. My contract with my phone carrier is up soon and I'm thinking I'm going to be getting a new phone. I'm really leaning towards Android so you might see some new Android tutorials. And I actually did a little bit of research and you can put Qt on Android. Somebody made a full port of it. Right in Qt Creator, it was actually really, really cool. So you can find that out on YouTube if you're looking for it. So QDOM element. What we're doing here is we're just getting the book out of our model here and then we are going to make a new element here. So we'll call this XML book. Equal document, create element. I'm kind of leaning away from the iPhone because I've heard really bad things about it and my girlfriend has an iPhone and as cool as it is, I just, I don't feel like learning how to develop for it. I already want to use the skill set that I have. And well, Android is Linux under the hood so it's actually really cool. And I've seen, I can't name names, but some folks that I work with, they've rooted their Androids and they put hacker tools on there. So you can run like Nessus and Metasploit and Nmap or write off your phone. So you have a mobile hacking station. It's actually really, really cool. I was totally impressed with that. Granted, it's slow because they're, well, they're phones, but it's just the fact that you can do that. It's just insane and it's a testament to where we are technologically. And what we're doing now, sorry, I'm babbling. I like to talk while I code. What we're doing now is just re-adding the elements in here. So we've got our name and we're adding the book text because there's our book, our key standard item from the model. So let's review really quick. We are not too deep into the code yet. We're getting the root element from the model. Then we're saying get each element out of the model. And I'm sure there's a more elegant way of doing this, but this is how we're doing it for this tutorial to keep it simple. And then we're just rebuilding that document, creating the element here. And we're adding the attributes. And then we're going to say XML root, append child and we're going to just append the XML book. I'm trying to remember, I think that was called a Cessitas and the lighthouse project of how you get cute on Android. And the little brief demo I watched on YouTube, it was just insane. Oh my gosh, it looks so cool. And if you can't tell by my excitement in my voice, I'm actually kind of geeked up to get an Android and try it. And yes, if I did, obviously we'd be making tutorials for it. All right, now H, what's that in the book? We want the row count from the book. And if you haven't guessed already, what we're doing here is we're getting the chapters out of the book. Now remember, this is the exact opposite of what we were doing. We're reading out of the model. And the reason why we're reading out of the model is because the models in memory and the models what's changed. And this is probably the most unelegant way of doing it. I'm sure there are better ways of doing it, but I really want to show by doing. I'm sure I'm going to get some hate emails from some of you pros out there that are admittedly better at this than I am. But I think it would be good for people to learn how to do this because admittedly it took me a while the first time. Then I'm very spoiled from .NET. I really miss the data set class in .NET where you can just load a data set and then save it right to an XML file. And I'm sure there's stuff out there in Qt that does that. All right, so now what we're doing is we're creating a Q standard item chapter and getting the book child at the position H because we're iterating over the rows. And then where you guessed it, we're just going to create a XML element here. So QDOM element. I'm going to have to pause and pet my cat here. She's giving me the evil eye. Give me one second. Come on kitty, you can wait one minute, can't you? The cat's shaking her head now. All right, cat, let go of my shirt sleeve. So we're creating a chapter and I'm going to have to pause this, I apologize. All right, we're back. Kitty's happy. Daddy's happy. We're just going to paste these in here and we're going to modify this here. And we'll say chapter. And what we're doing is pretty much the same thing with the book, we're just setting the chapter text, the ID, make sure that's the H, not the I. So we've got our chapter, we're setting the attributes, then we're pending this to the book element. Now, once we've got all that done, we need to jump out of these loops here and you guessed it, save to disk. Now, if you have a fairly large document, you would want to kind of buffer this out and save it in chunks. But for this example, we've got a very small document and we don't need to do that. So let's go file, file name. And we're going to do file open, QIO device, right only. And we want to do this in text mode. So we're going to just very simply add a comment here saying if we cannot do this, then we want to cutie bug out and say, hey, there was a problem. And admittedly in these tutorials for the sake of speed, I haven't been doing as much error checking as I probably should. And I've gotten a couple nasty grams from people saying, hey, add some error checking. Show some debugging, show how to do this stuff. All right, so now we're going to create our stream. If you have no idea how to do this, well, once again, I implore you to watch our previous tutorials. I would love to cover all of this stuff all in one shot, but it would do two things. It would make this video about four hours long and it would bore people to death who have been religiously watching the videos. So here we go, two string. So what we're going to do here is just take the document, convert it to a string, dump it into this text stream, and then close the file. And then we just want to do a cutie bug finish. That way we know it was actually written out to disk. Save our work here. And let's run this. Uh-oh, we got a boo-boo. Base adder and non-parter and dom, are you? Hmm. Okay, let me pause this and figure out what's going on. Dur, I was trying to do the wrong version there. There we go. Now let's run this. So yeah, I've gotten some really cool ideas from people about what the 100th video should be, and I want to do something special. All right, now, you know, the boss can do that and we'll do book one. We'll just say LOL, and we'll say cats, dogs, mice, and, hmm, need another animal. Fish, why not? So as you see, we can edit this, and even though we've edited this root thing, that will not be saved. Let me click the save button. You see how it says finished? Grab our document and let's just F5 to refresh. You notice how books stayed the same, but the elements have changed. We now have book one, LOL, cats, dogs, mice. So we've written this out to disk. So now the boss closes the program, reruns it, and notice how books is back, because we don't want the boss to overwrite that, and we've got our changes right here. So that, in a nutshell, is how to make a basic XML editor. Now, unfortunately, making a full-blown XML editor that could modify and edit any XML document on the planet would take a long, long time. So this is the best I could do on short notice, and I apologize if this tutorial's extremely long. So thank you for watching. I hope you found this tutorial educational and entertaining, and if you liked this tutorial, oh, hold on. Sorry, Kitty was getting sick. Poor cat. If you like this tutorial, go out to my website, www.voidrealms.com, click tutorials, and you can go to cute, and you can search, or you can just go for the last one, and basically when you go in here, you can get the actual source code along with watch the videos. If you go to download this, there is a next screen saying, hey, look, the site's run by donations only. Please don't feel obligated to give a donation. I have been getting donations, and my website is 100% funded off donations. I appreciate your donations, but don't feel obligated. Please, I've had a couple of people saying, is it necessary? No, it is not. I do this because I want to do it. Other than that, I'm really looking for feedback on the 100th video. I've got a couple really, really good ideas from people, and I'm really looking forward to doing it. So as always, this is Brian. Thank you for watching, and I promise, and I apologize at the same time. I will get to your messages if you sent me one. I've got a backlog of about a month and a half, two months, and it's overwhelming, but I'm really trying to plow through them. Thanks for watching. Bye.