教程:Qt Quick 调试

本教程使用Same Game示例应用程序说明如何在Debug 模式下调试Qt Quick 应用程序。

有关所有选项的详细信息,请参阅调试Qt Quick 项目

Same Game 演示展示了如何使用 QML 编写游戏,并使用 JavaScript 编写所有游戏逻辑。在Qt Creator 中打开演示项目进行调试:

  1. 要查看启动新游戏的代码,可在 samegame.qml 中设置一个断点,方法是点击调用startNewGame() 函数的行 (1) 上的行号和窗口边框之间。

    代码编辑器中的断点

    红圈表示断点已设置在该行号上。

  2. 转到Debug >Start Debugging >Start Debugging of Startup Project ,或按F5
  3. 启动 Same Game 程序后,选择Puzzle 开始新游戏。

    同一游戏应用程序

  4. 当调试器碰到断点时,就会中断应用程序。Qt Creator 以调用堆栈跟踪的形式显示导致当前位置的嵌套函数调用。

    调试器视图

  5. 选择工具栏上的走进 (Step Into) 或选择F11,即可进入堆栈中的代码。在代码编辑器中打开 samegame.js 文件,找到启动新游戏的函数。

    堆栈视图

  6. Locals 视图中查看局部变量。逐步浏览代码,查看视图中的信息是如何变化的。
  7. startNewGame() 函数的末尾添加一个断点,然后选择继续 (Continue) 来触发断点。

    断点视图中的第二个断点

  8. 要在当前上下文中执行 JavaScript 命令,请打开QML Debugger Console

    QML 调试器控制台

  9. 要移除断点,请右键单击该断点并选择Delete Breakpoint
  10. Locals 视图中,探索运行时的对象结构。

    当地人的观点

  11. 转到Debug >Show Application on Top ,以便在与调试器交互时保持应用程序可见。
  12. 转到Debug >Select 激活选择模式,然后选择Menu 进入Locals 视图和代码编辑器中的menuButton 组件。
  13. Locals 视图中,双击属性值可对其进行更改。

另请参阅 调试Qt Quick 项目

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.