使用适当的功能

此警告类别由 qmllint 拼写[use-proper-function]

Property 是一个变量属性:它可能是也可能不是方法

发生了什么?

你把var 类型的属性当作可调用函数使用。

为什么这样做不好?

这会影响代码的可读性,如果属性不包含可调用函数,QML 引擎会出错,而且 QML 工具无法应用特定的方法优化。

示例

import QtQuick

Item {
    property var fun: () => console.log("Hello")
    Component.onCompleted: { fun(); }
}

要修复此警告,请将fun 声明为一个函数,以便能调用它:

import QtQuick

Item {
    function fun() {
        console.log("Hello")
    }

    Component.onCompleted: { fun(); }
}

Property 是 QJSValue 属性:它可能是也可能不是一个方法

发生了什么事?

您将QJSValue 类型的属性当作可调用函数使用。

注意: QJSValue 类型的属性只能在 C++ 中定义。

为什么这样做不好?

这个属性很可能不是用来调用的,会让 QML Runtime 引擎在运行时出错。

示例

import QtQuick

Rectangle {
    // Rectangle has a property gradient of the type QJSValue
    Component.onCompleted: { console.log(gradient()); }
}

要修复此警告,请删除对gradient 的调用:

import QtQuick

Item {
    // Rectangle has a property gradient of the type QJSValue
    Component.onCompleted: { console.log(gradient); }
}

或者,如果您是属性的作者,可考虑用Q_INVOKABLE 方法或代替属性定义。这只有在 QML 中永远不会改变函数的情况下才有可能。如果你真的需要存储回调,那就没戏了。

信号被属性遮挡

发生了什么?

你调用了一个被属性遮挡的信号,或者你用属性遮挡了一个信号。

为什么会这样?

调用者很可能期望调用的是信号,而不是阴影属性。这会让 QML Runtime 引擎在运行时出错。

示例

import QtQuick

Item {
    component Base: Item {
        signal helloSignal
    }

    Base {
        property int helloSignal // shadows helloSignal inherited from Base
        Component.onCompleted: { helloSignal(); }
    }
}

要修复此警告,请重命名阴影属性:

import QtQuick

Item {
    component Base: Item {
        signal helloSignal
    }

    Base {
        property int helloSignal2 // does not shadow anymore
        Component.onCompleted: { helloSignal(); }
    }
}

方法被一个属性遮挡

请参阅 {信号被一个属性遮挡}。

槽被一个属性阴影覆盖

请参见{信号被一个属性遮挡}。

属性不是方法

发生了什么?

你把一个非varQJSValue 类型的属性当作可调用函数来使用。

为什么这样做不好?

该属性不能被调用,会让 QML Runtime 引擎在运行时出错。

示例

import QtQuick

Item {
    property int hello
    Component.onCompleted: { console.log(hello()); }
}

要修复此警告,请删除调用或将hello 设为函数:

import QtQuick

Item {
    property int hello
    Component.onCompleted: { console.log(hello); }

    // alternative:
    function helloAlternative() { ... }
    Component.onCompleted: { console.log(helloAlternative()); }
}

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