| Tutorial | Classes | Functions | QSA Developer | Language | Library | Qt API | Qt Script for Applications | ![]() |
[Prev: Creating Scripts using Qt Script for Applications] [Home] [Next: Deploying QSA-enabled Applications]
Qt Script for Applications Developer includes an integrated debugger. The debugger helps you locate and correct programming errors. You can set breakpoints which allow you to run to specific points in the code from where you can single step and watch variables as they change. The debugger provides views of the call stack and variables. This chapter demonstrates how to debug Qt Script for Applications scripts, using the Convert to Euro script as an example.
Script execution stops at breakpoints. Once the interpreter stops executing, it is possible to view the values of the variables that are within scope. The following steps show you how to set a breakpoint.
Open the Convert to Euro dialog you created in the previous chapter. In the File Overview window, click EuroSettings [Source] to open the main form and its code.
In the source code editor, find the buttonCalculate_clicked() function. Go to the first line of code in the function, in this case, var dividor. Click to the left of the vertical line to set a breakpoint. The breakpoint is shown as a red stop sign. You can also move the cursor to the line where you want to set the breakpoint and then click the Breakpoint toolbar button (F9).
Setting a Breakpoint
Click the Run Function toolbar button to invoke the Choose a function to run dialog.
Choose a function to run dialog
Choose the convertToEuro function from the combobox and click OK to run it. The Settings for Euro Converter dialog will appear. Click Calculate. Notice that the line of code at which the breakpoint is set, is now highlighted. The script has stopped executing and the debugger is ready to step through the code. The Call Stack Window and the Watch Variables Window appear automatically when you click Run Function. We will discuss the functionality of these windows later on in the chapter as we step through the code.
Execution Stops at Breakpoints
When the code execution reaches a breakpoint, the script stops running. The line containing the breakpoint is highlighted. The debugger is now ready for you to step through the code. When stepping through a line of code, that line is executed and you can see the effects of this by viewing the Call Stack Window and Watch Variables Windows. When stepping through code, you can either step over, or step into the code.
Stepping Over Toolbar Button
When you step over a line of code, that line is executed and the step arrow (blue arrow) moves to point to the next statement in the code. If the statement that is executed is a function call, the call will be executed but the debugger will not step into the called function.
We will use the Convert to Euro example to demonstrate stepping over. At this point, you should have set and run to the breakpoint, so that the line of code at the breakpoint is highlighted. Click the Step Over toolbar button (or press F10). The highlighted statement is executed and then the step arrow appears to the left of the line. Continue to click Step over to move to the next statement in the code.
Stepping Over Each Line of Code
Stepping Into Toolbar Button
The Convert to Euro script example contains only one function so we cannot demonstrate stepping into another function. However, stepping into is a concept that is important to know when debugging.
When the step arrow is pointing to a function call, the function will be executed and the debugger will move to the following statement. If you click the Step Into toolbar button, the function will be called and the step arrow will be positioned at the first executable line of the function that was called.
The Call Stack window lists functions that are currently loaded from the breakpoint which you have set. Call stacks are useful in debugging since they show which function a particular function is called from. The call stack window has two columns, the Function column and the Line column. The function name is listed in the function column and the line of code in the file where the function was called is listed in the line column. For all functions except the last one in the call stack, the line column shows the line number at which the function call was made. For the last function listed in the stack, the number represents the line where execution stopped.
For our example, look at the call stack window and you will see a stack of two functions. If you click on one of the functions, the cursor will point to the line in the code where the function call was made.
Using the Call Stack Window
The Watch Variables window is used to view and change the values of variables as you debug the program. The window consists of the Variable column, the Value column and the Type column. The Variable column is separated into Global Variables and Local Variables. The variable names are listed below the appropriate variable column. The Value column shows the current value of each variable. The Type column shows what each variable's type is.
Shortcuts for Watching Variables
A quick shortcut for finding out a variable's value and type is to view the tooltips that appear when you hover over a variable name in the editor window.
The debugger also has a shortcut for automatically adding variable names to the Variables column in the Watch Variables window. To add a variable name, set your breakpoint and start the debugger. Press Ctrl and move your mouse over the code. Notice that any code that the mouse moves over is highlighted. Highlight a variable name you want to select, click on it and it will appear in the variables column.
We will use the Convert to Euro example to watch a local variable, in this case, dividor. Follow the steps above to set the breakpoint and stop at the breakpoint. Step over one time and notice that the dividor variable is listed as 'undefined' in the Values column of the Watch Variables window. Step over two more times and you will see that the value is now set to the value written in the source code editor.
Watch Variables Window
Watching the 'dividor' Local Variable
The first couple of chapters explained how to create a Qt Script for Applications script, and this chapter has demonstrated the essentials of using Qt Script for Applications Developer's debugger. You now have enough information and practical experience to create your own Qt Script for Applications scripts. The Language Reference provides more information on the Qt Script for Applications scripting language, and the Library Reference covers all the classes and functions that are available to you. We recommend that you read the Language Reference, and skim the Library Reference to get a feel of what Qt Script for Applications has to offer (and to avoid reinventing the wheel).
We hope that you enjoy using Qt Script for Applications, and produce great scripts with it!
[Prev: Creating Scripts using Qt Script for Applications] [Home] [Next: Deploying QSA-enabled Applications]
Copyright © 2001-2002 Trolltech | Trademarks | QSA version 1.0.0-beta1
|