Okay, so, you just got handed a job for a 100+ page workbook. Your client wants you to fix some formatting or something of the like on every page (the 100 pages already exist… creating that many is a different story…). What do you do? You use macros and let the computer do all the work, silly! 😛 The best part is you can charge for all the hours it would have taken you to fix it all up without the snippet I’m about to give you! (Okay, this is probably a tad unethical…).
You’re interested? I thought so. We’re going to use the power of foreach loops to let the computer run through and do all this on every single page. The workflow I use looks something like this:
On the first page (or a “test” page), I hit Record Macro and name it something I can remember. Be sure that you remember what the exact name of it is. This is very important.
Proceed to style/do whatever it is that you’re being paid to do. Ahem.
Once you’re done, stop recording the macro. At this point, I usually like to test the macro before I use it on my entire workbook, just to make sure it isn’t going to destroy it all. 😛
If you have verified the integrity, now we need to write a bit of code. :O But I have never used the Visual Basic editor! Don’t worry–it’s incredibly simple and you can basically copy and paste this snippet I’m about to give you and just change one little bit for all your repeating macros.
Today, we’ll look at a quick tip to show you how to use the Qt Creator Signals and Slots editor to add a checkbox that disables/enables widgets on a form. Of course, you could easily do this in code, but this is one of those things that I simply prefer to use the form editor for, since it creates a lot of redundant code in your UI class.
The logic behind it is simple. You have a checkbox toggled(bool) signal connected up to your widget with the setEnabled(bool) slot. First, go to the editor and change it to signals and slots mode. Drag a connection from the checkbox/toggle to the widget(s) you would like to have toggled. When they connect, make sure you have inherited signals/slots checked! You will not get the setEnabled(bool) otherwise.
Make sure you have the inherited signals/slots.
This form totally won’t be used on this blog… 😉
And all this without cluttering your UI class! Below you can see it in action. I would encourage you to play around with some of the other slots, namely setHidden(bool), which might give you a result you are looking for.
Drag and drop is probably one of my favorite way of interacting with an interface. It’s just intuitive and works nicely. Implementing custom dragging and dropping events isn’t hard at all, as you can easily leverage the power of the Qt Event system with custom filters to customize behavior.
In this particular example, we’ll use a sub-classed version of a QPlainTextEdit and reimplement the Drop Event virtual protected method. If the user drags a text file on it, we want to open the file and output the text to the widget. Now, you could probably do this with a custom event filter, but this is a tutorial and really shouldn’t be taken literally for use. I’ll probably go over installing event filters another time, but sub-classing is an important skill to learn–especially in Qt for customizing behavior!
Before we get to the code, lets go over what happens. When you drag and drop something, several events are triggered. You can customize every aspect of it. For instance, when you are dragging a file over your application’s window, you could draw attention to the drop zone or something along those lines. However, we are mainly interested in the event when the user drops a file on our widget.
In the first part of this tutorial series, we went over the main.cpp file and a brief overview of what we wanted to accomplish in this application. Now we’ll go over the MainWindow class and save the state upon close of the window. First, we’ll look at the header file:
You have probably used an application that restores state and/or geometry at some point. State means how things “are” in an application, to so sum it up. Geometry is where the window was on the screen, and the size. We’ll be using some Qt functions that take care of most of the actual settings work. I’ll also show you how to override the closing event of a QMainWindow so you can inject custom code into the virtual event. For example, say you are making a text editor and you want to save the current files that are open. In addition, you’ll learn a few other things along the way, while the main focus will be QSettings.