调试 QML 应用程序

当你用 QML 开发应用程序时,有许多方法可以调试可能遇到的问题。下面的章节介绍了可用的调试工具和使用方法。

控制台 API

功能说明
日志使用console.log,console.debug,console.info,console.warnconsole.error 将调试信息打印到控制台。

例如

function f(a, b) {
  console.log("a is ", a, "b is ", b);
}

输出是使用 C++ 中的qCDebugqCWarningqCCritical 方法生成的,类别为qmljs ,具体取决于进行日志记录的文件类型。另请参阅调试技巧

断言console.assert 测试表达式是否为真。如果不是,它将向控制台写入一条可选信息并打印堆栈跟踪。

例如

function f() {
  var x = 12
  console.assert(x == 12, "This will pass");
  console.assert(x > 12, "This will fail");
}
计时器console.time 和 记录调用之间所用的时间(以毫秒为单位)。两者都使用一个字符串参数来标识测量值。console.timeEnd

例如

function f() {
    console.time("wholeFunction");
    console.time("firstPart");
    // first part
    console.timeEnd("firstPart");
    // second part
    console.timeEnd("wholeFunction");
}
跟踪console.trace 会打印 JavaScript 被调用时的堆栈跟踪。堆栈跟踪信息包括函数名、文件名、行号和列号。堆栈跟踪仅限于最后 10 个堆栈帧。
计数console.count 打印特定代码的当前运行次数,并附带一条信息。

例如

function f() {
  console.count("f called");
}

上面的代码示例会在运行f() 时打印f called: 1,f called: 2... 。

配置文件console.profile 会打开 QML Profiler 和 JavaScript Profiler。不支持嵌套调用,并在控制台打印警告。
ProfileEndconsole.profileEnd 会关闭 QML 和 JavaScript 剖析器。在未调用 的情况下调用该函数,会在控制台打印警告。在调用该函数之前,需要附加剖析客户端以接收和存储剖析数据。console.profile

例如

function f() {
    console.profile();
    //Call some function that needs to be profiled.
    //Ensure that a client is attached before ending
    //the profiling session.
    console.profileEnd();
}
异常console.exception 会在调用时打印出错信息以及 JavaScript 的堆栈执行跟踪。

另外,也可以将logging category 作为第一个参数传递给这些console 函数。详情请参见LoggingCategory

调试模块导入

设置QML_IMPORT_TRACE 环境变量,启用 QML 导入加载机制的调试输出。

例如,对于这样一个简单的 QML 文件:

import QtQuick

Rectangle { width: 100; height: 100 }

如果您在运行QML Runtime 工具或 QML C++ 应用程序之前设置QML_IMPORT_TRACE=1 ,您将看到类似的输出:

QQmlImportDatabase::addImportPath "/qt-sdk/imports"
QQmlImportDatabase::addImportPath "/qt-sdk/bin/QMLViewer.app/Contents/MacOS"
QQmlImportDatabase::addToImport 0x106237370 "." -1.-1 File as ""
QQmlImportDatabase::addToImport 0x106237370 "Qt" 4.7 Library as ""
QQmlImportDatabase::resolveType "Rectangle" = "QDeclarativeRectangle"

QML 调试基础结构

Qt Qml模块通过 TCP 端口或本地套接字为调试、检查和剖析应用程序提供服务。

注意: 在设备上调试和剖析 QML 应用程序所需的qmltooling 插件会在安装 Qt 时自动安装。它们必须部署到设备上,才能进行调试和剖析。

启用基础架构

编译应用程序时,必须明确启用调试基础结构。如果使用 qmake,可以在项目.pro 文件中添加配置参数:

  • Qt Quick 1:CONFIG+=declarative_debug
  • Qt Quick 2:CONFIG+=qml_debug

如果使用其他编译系统,可以将以下定义传递给编译器:

  • Qt Quick 1:QT_DECLARATIVE_DEBUG
  • Qt Quick 2:QT_QML_DEBUG

注意: 启用调试基础结构可能会损害应用程序和系统的完整性,因此只能在受控环境下启用。启用基础结构后,应用程序会显示以下警告:

QML debugging is enabled. Only use this in a safe environment.

启动应用程序

要从一开始就启用调试或稍后附加调试器,请使用以下参数启动应用程序:

-qmljsdebugger=port:<port_from>[,port_to][,host:<ip address>][,block][,file:<local socket>][,services:<comma-separated list of services to enable>]

其中

  • 必选的port_from 指定调试端口或指定port_to 时端口范围的起始端口
  • 可选的ip address 指定运行应用程序的主机的 IP 地址
  • 可选的block 阻止应用程序运行,直至调试客户端连接到服务器
  • 可选项file 指定本地套接字。
  • 可选项services 指定要启用的服务;默认情况下启用所有找到的服务。请注意,v4 debug 服务将禁用 JIT。

应用程序成功启动后,将显示以下消息:

QML Debugger: Waiting for connection on port <port_number>QML Debugger: Connecting to socket at <file>"

连接到应用程序

当应用程序运行时,集成开发环境或实现二进制协议的工具可以连接到开放端口。

Qt XML 提供了一个qmlprofiler 命令行工具,用于在文件中捕获剖析数据。要运行该工具,请输入以下命令:

qmlprofiler -p <port> -attach <ip address>

调试Qt Creator

Qt Creator Qt Creator 提供了调试 JavaScript、检查对象树和剖析 QML 引擎活动的集成客户端。使用调试基础架构可在桌面和远程设备上调试、检查和剖析 应用程序。更多信息,请参阅 :Qt Quick Qt Creator 调试 项目Qt Quick

© 2025 The Qt Company Ltd. 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.