QScriptEngineDebugger Class | Qt Script Tools

Qt 5.9 | Qt Script Tools | C++ Classes | QScriptEngineDebugger

Qt 5.9.3 Reference Documentation

# QScriptEngineDebugger Class

The QScriptEngineDebugger class provides a QScriptEngine debugger. More...

Header: | #include <QScriptEngineDebugger>
qmake: | QT += scripttools
Since: | Qt 4.5

- List of all members, including inherited members

## Public Types

enum | DebuggerAction { InterruptAction, ContinueAction, StepIntoAction, StepOverAction, ..., GoToLineAction }
enum | DebuggerState { RunningState, SuspendedState }
enum | DebuggerWidget { ConsoleWidget, StackWidget, ScriptsWidget, LocalsWidget, ..., ErrorLogWidget }

## Public Functions

| QScriptEngineDebugger(QObject *parent = Q_NULLPTR)
| ~QScriptEngineDebugger()
QAction * | action(DebuggerAction action) const
void | attachTo(QScriptEngine *engine)
bool | autoShowStandardWindow() const
QMenu * | createStandardMenu(QWidget *parent = Q_NULLPTR)
QToolBar * | createStandardToolBar(QWidget *parent = Q_NULLPTR)
void | detach()
void | setAutoShowStandardWindow(bool autoShow)
QMainWindow * | standardWindow() const
DebuggerState | state() const
QWidget * | widget(DebuggerWidget widget) const

## Signals

void | evaluationResumed()
void | evaluationSuspended()

## Detailed Description

The QScriptEngineDebugger class provides a QScriptEngine debugger.

The QScriptEngineDebugger class provides a debugger that can be embedded into Qt applications that use Qt Script. The debugger enables the application user to inspect the state of the script environment and control script execution.

To attach the debugger to a script engine, call the attachTo() function.

          QScriptEngine engine;
          QScriptEngineDebugger debugger;
          debugger.attachTo(&engine);

Once the debugger has been attached to a script engine, you can proceed to evaluate scripts as usual, e.g. by calling QScriptEngine::evaluate(). The debugger will be triggered when an uncaught exception occurs, or when a `debugger` statement is encountered in a script. It is also possible to interrupt script evaluation at an arbitrary time by triggering the InterruptAction. For instance, to start the debugger when script evaluation starts, you trigger the action before you begin to evaluate() the script.

           debugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
           engine->evaluate(contents, fileName);

By default, the standard debugger window is shown when evaluation is suspended. This can be changed by calling the setAutoShowStandardWindow() function.

The debugger defines a set of actions that are available, such as stopping execution or printing the contents of a variable. It also provides a set of widgets (components) that display the information available from the debugger and that trigger the actions on request. The actions available are identified by the DebuggerAction enum, and the widgets are identified by the DebuggerWidget enum.

Access to the individual debugger widgets is provided by the widget() function. This makes it possible to arrange the widgets in a custom manner. Similarly, the action() function provides access to the various debugger actions.

The createStandardToolBar() function creates a standard toolbar, and the createStandardMenu() function creates a standard menu; these functions can be useful if you are creating a custom debugger configuration.

The evaluationSuspended() signal is emitted when the debugger has suspended script evaluation and entered interactive mode, i.e., the mode in which it accepts input from the user. The evaluationResumed() signal is emitted when script evaluation is resumed, i.e, when execution control is given back to the script engine. The state() function returns the debugger's current state.

When calling QScriptEngine::evaluate() it is useful to pass a descriptive script name (file name) as second argument, as this is the name that will be displayed by the debugger in the ScriptsWidget; if a name is not passed, the script will be labelled "anonymous".

When evaluation is suspended, the debugger will also suspend the event loop of the script. In the following snippet, the call to QScriptEngine::evaluate() causes the debugger to be triggered, and the function call does not return until the user has finished interacting with the debugger.

          engine.evaluate("debugger");

When the Qt Script debugger is running, the C++ application itself is not "frozen". This means that it is possible that more scripts are evaluated, even though the debugger has suspended evaluation of the current script evaluation. For example, a C++ timer might trigger that causes a script function to be called, or the user might click on a button in the main application user interface whose clicked() signal is connected to a script function. This kind of nested evaluation is permitted. The debugger will enter interactive mode for the new script if an exception is thrown or a breakpoint is reached. Note that it will not stop when encountering `debugger` statements.

Nested evaluation requires some thought when deciding how the debugger is presented to the user; for example, whether a modal dialog is suitable, or whether some parts of the main application user interface should be disabled while the debugger is running.

Debugging inside of a paintEvent() is currently not supported. If you need to debug painting-related script code, that code should be evaluated outside of the C++ paintEvent(), e.g. by rendering to an image, like the Context2D and Tetrix Qt Script examples do. This will make the code safe for debugging.

The debugger adds some special properties to the script engine: `__FILE__` holds the name of the script in which the current evaluation occurs, and `__LINE__` holds the current line number. These are useful when doing print()-style debugging (the messages appear in the debugger's debug output widget).

The Qt Script Debugger Manual describes how to use the debugger. The Context2D example shows how to integrate the debugger in applications.

See also QScriptEngine and Context2D Example.

## Member Type Documentation

### enum QScriptEngineDebugger::DebuggerAction

This enum specifies the action that the action() function should retrieve. The actions retrieved can be connected to any slot and connected to any widget. Please see the Qt Script Debugger Manual's Console Command Reference for a detailed description of these actions.

Constant | Value | Description
--- | --- | ---
`QScriptEngineDebugger::InterruptAction` | `0` | Suspends script execution as soon as the next script statement is reached.
`QScriptEngineDebugger::ContinueAction` | `1` | Gives the execution control back to the script engine.
`QScriptEngineDebugger::StepIntoAction` | `2` | Performs a step action.
`QScriptEngineDebugger::StepOverAction` | `3` | Performs a next action.
`QScriptEngineDebugger::StepOutAction` | `4` | Executes the script until the current function returns.
`QScriptEngineDebugger::RunToCursorAction` | `5` | Continues execution to the selected line (which contains the cursor) in the CodeWidget.
`QScriptEngineDebugger::RunToNewScriptAction` | `6` | Returns control to the script engine until a new script is executed.
`QScriptEngineDebugger::ToggleBreakpointAction` | `7` | Toggles a breakpoint at the selected line in the CodeWidget.
`QScriptEngineDebugger::ClearDebugOutputAction` | `8` | Clears the contents of the DebugOutputWidget.
`QScriptEngineDebugger::ClearErrorLogAction` | `9` | Clears the contents of the ErrorLogWidget.
`QScriptEngineDebugger::ClearConsoleAction` | `10` | Clears the contents of the ConsoleWidget.
`QScriptEngineDebugger::FindInScriptAction` | `11` | Displays the CodeFinderWidget.
`QScriptEngineDebugger::FindNextInScriptAction` | `12` | Finds next occurrence in the CodeWidget.
`QScriptEngineDebugger::FindPreviousInScriptAction` | `13` | Finds previous occurrence in the CodeWidget.
`QScriptEngineDebugger::GoToLineAction` | `14` | Shows the "Go to Line" dialog.

### enum QScriptEngineDebugger::DebuggerState

This enum specifies the current state of the debugger.

Constant | Value | Description
--- | --- | ---
`QScriptEngineDebugger::RunningState` | `0` | The debugger is running. (Script evaluation is allowed.)
`QScriptEngineDebugger::SuspendedState` | `1` | The debugger has suspended script evaluation.

This enum was introduced or modified in Qt 4.6.

### enum QScriptEngineDebugger::DebuggerWidget

This enum decides the widget that the widget() function should retrieve. We treat these widgets in more detail in the Qt Script Debugger Manual.

Constant | Value | Description
--- | --- | ---
`QScriptEngineDebugger::ConsoleWidget` | `0` | Provides a command-line interface to the debugger.
`QScriptEngineDebugger::StackWidget` | `1` | Shows a backtrace of the script's execution state.
`QScriptEngineDebugger::ScriptsWidget` | `2` | Displays a list of currently loaded scripts.
`QScriptEngineDebugger::LocalsWidget` | `3` | Shows the local variables of the current stack frame.
`QScriptEngineDebugger::CodeWidget` | `4` | Displays the code of the current script.
`QScriptEngineDebugger::CodeFinderWidget` | `5` | Provides a widget that can search for text in the script shown in the CodeWidget.
`QScriptEngineDebugger::BreakpointsWidget` | `6` | Shows breakpoints that have been set.
`QScriptEngineDebugger::DebugOutputWidget` | `7` | Contains output from the `print()` script function.
`QScriptEngineDebugger::ErrorLogWidget` | `8` | Shows error messages that have been generated.

## Member Function Documentation

### QScriptEngineDebugger::QScriptEngineDebugger(QObject *parent = Q_NULLPTR)

Constructs a new QScriptEngineDebugger object with the given parent.

To attach a QScriptEngine to the debugger, use attachTo() function.

### QScriptEngineDebugger::~QScriptEngineDebugger()

Destroys this QScriptEngineDebugger.

### QAction *QScriptEngineDebugger::action(DebuggerAction action) const

Returns a pointer to the specified action. The actions available are given by the DebuggerAction enum.

With this function, you can add the actions to your own widgets, toolbars, and menus. It is also convenient if you, for example, wish to spice things up with your own groovy icons. The code example below shows how to add actions to a QToolBar.

          QAction *continueAction = debugger->action(QScriptEngineDebugger::ContinueAction);
          QAction *stepOverAction = debugger->action(QScriptEngineDebugger::StepOverAction);
          QAction *stepIntoAction = debugger->action(QScriptEngineDebugger::StepIntoAction);

          QToolBar *toolBar = new QToolBar;
          toolBar->addAction(continueAction);

Note that QScriptEngineDebugger has already added the actions to its standard widgets and standard window.

See also widget(), createStandardMenu(), createStandardToolBar(), and standardWindow().

### void QScriptEngineDebugger::attachTo(QScriptEngine *engine)

Attaches to the given engine.

The debugger will install a custom agent (using QScriptEngine::setAgent()) to monitor the engine. While the debugger is attached, you should not change the agent; however, if you do have to perform additional monitoring, you must set a proxy agent that forwards all events to the debugger's agent.

See also detach().

### bool QScriptEngineDebugger::autoShowStandardWindow() const

Returns whether the standard debugger window is automatically shown when evaluation is suspended.

The default is true.

See also setAutoShowStandardWindow().

### QMenu *QScriptEngineDebugger::createStandardMenu(QWidget *parent = Q_NULLPTR)

Creates a standard debugger menu with the given parent. Returns the new menu object.

See also createStandardToolBar().

### QToolBar *QScriptEngineDebugger::createStandardToolBar(QWidget *parent = Q_NULLPTR)

Creates a standard debugger toolbar with the given parent. Returns the new toolbar object.

See also createStandardMenu().

### void QScriptEngineDebugger::detach()

Detaches from the current script engine, if any.

See also attachTo().

### `[signal]` void QScriptEngineDebugger::evaluationResumed()

This signal is emitted when the debugger has resumed script evaluation (e.g. the user gave the "continue" command).

See also evaluationSuspended().

### `[signal]` void QScriptEngineDebugger::evaluationSuspended()

This signal is emitted when the debugger has suspended script evaluation for whatever reason (e.g. due to an uncaught script exception, or due to a breakpoint being triggered).

See also evaluationResumed().

### void QScriptEngineDebugger::setAutoShowStandardWindow(bool autoShow)

Sets whether the standard debugger window is automatically shown when evaluation is suspended. If autoShow is true, the window will be automatically shown, otherwise it will not.

See also autoShowStandardWindow().

### QMainWindow *QScriptEngineDebugger::standardWindow() const

Returns a main window with a standard configuration of the debugger's components.

See also createStandardMenu() and createStandardToolBar().

### DebuggerState QScriptEngineDebugger::state() const

Returns the current state of the debugger.

This function was introduced in Qt 4.6.

See also evaluationResumed() and evaluationSuspended().

### QWidget *QScriptEngineDebugger::widget(DebuggerWidget widget) const

Returns a pointer to the instance of the specified standard widget. The widgets available are defined by the DebuggerWidget enum.

A main window containing all widgets is returned by standardWindow(). If you do not want to use this window, you can fetch the individual widgets with this function. For instance, the code example below shows how to set up a layout containing a code window and a stack widget.

          QWidget *codeWindow = debugger->widget(QScriptEngineDebugger::CodeWidget);
          QWidget *stackWidget = debugger->widget(QScriptEngineDebugger::StackWidget);

          QLayout *layout = new QHBoxLayout;
          layout->addWidget(codeWindow);
          layout->addWidget(stackWidget);

Note that you need to set setAutoShowStandardWindow() to false; if not, the standard window will be shown regardless.

See also action(), standardWindow(), and setAutoShowStandardWindow().