本页内容

在声明之前就使用了该函数

qmllint 将此警告类别拼写为“[function-used-before-declaration] ”。

该警告类别默认处于禁用状态。

在声明之前使用函数

发生了什么?

您在函数声明之前就调用了该函数或使用了其名称。

为什么这样不好?

这会使代码更难阅读,并可能造成混淆。

请注意,由于提升机制,该函数在声明之前就已经可用。

示例

import QtQuick

Item {
    function f() {
        g(42)
        function g() { return 42; }
    }
}

要修复此警告,请将声明移至使用位置之前。

import QtQuick

Item {
    function f() {
        function g() { return 42; }
        g(42)
    }
}

© 2026 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.