调试 QML 应用程序

一、控制台接口

1.1、Log

使用 console.log、console.debug、console.info、console.warn 或 console.error 将调试信息打印到控制台。

Window
{
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            console.log("hello world")
            console.debug("hello world")
            console.info("hello world")
            console.warn("hello world")
            console.error("hello world")
        }
    }
}

1.2、Assert

console.assert 测试表达式是否为真。如果没有,它会向控制台写入可选消息并打印堆栈跟踪。

1.3、Timer

console.time 和 console.timeEnd 记录调用之间花费的时间(以毫秒为单位)。两者都采用标识测量的字符串参数。

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            console.time("wholeFunction");
            console.time("firstPart");
            for(var i = 0;i < 100;++i)
            {}
            console.timeEnd("firstPart");
            for(var i = 0;i < 10000;++i)
            {}
            console.timeEnd("wholeFunction");
        }
    }

1.4、Trace

console.trace 在调用它的地方打印 JavaScript 执行的堆栈跟踪。此堆栈跟踪信息包含函数名、文件名、行号和列号。堆栈跟踪仅限于最后 10 个堆栈帧。

1.5、Count

console.count 打印特定代码段的当前运行次数以及一条消息。

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            console.count("onClicked called");
        }
    }

1.6、Exception

console.exception 会在调用它时打印一条错误消息以及 JavaScript 执行的堆栈跟踪。

二、调试模块导入

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

设置 QML_IMPORT_TRACE=1,以下代码会输出:

import QtQuick 2.14
import QtQuick.Window 2.14

Window
{
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
        }
    }
}

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值