QML 自定义按钮无法发出按下信号,因为它不是函数?

QML Custom Button can't emit pressed signal cause it is not a function?

我正在尝试实现一个自定义按钮,它具有 press()release() 功能,当收到预期的按键事件时我会调用这些功能。在这些函数中,调用了 pressed()released() 信号。 released() 完美运行,但是当调用 pressed() 时显示错误:

TypeError: Property 'pressed' of object CustomButton_QMLTYPE_3(0x78214d8) is not a function

我的理论是 QML 无法区分按钮的 bool pressed 属性 和 pressed() 信号。这是一个错误还是我做错了什么?这是我所做的:

这是自定义按钮 qml 文件:

import QtQuick 2.10
import QtQuick.Controls 2.12

Button {
    id: control

    function press() {
        down = true;
        pressed();
    }

    function release() {
        down = false;
        released()
    }
}

在下面的示例中,当按下或释放 F3 键时,我调用按钮函数,我希望它们到达我创建的连接。

 CustomButton {
        id: customButton
        width: parent.width
        height: parent.height

        Connections {
            target: customButton
            onPressed: {
                console.log("Custom button pressed!\n");
            }

            onReleased: {
                console.log("Custom button released!\n")
            }
        }
    }

    focus: true
    Keys.onPressed: {
        if(event.key === Qt.Key_F3 && !event.isAutoRepeat) {
            console.log("F3 Key pressed!")
            customButton.press()
        }
    }

    Keys.onReleased: {
        if(event.key === Qt.Key_F3 && !event.isAutoRepeat) {
            console.log("F3 Key released!")
            customButton.release()
        }
    }

就像我说的那样发布有效,但媒体有问题。我在控制台中看到这些行:

qml: F3 Key pressed!
qml: press function called
file:///D:/Projects/QmlExamples/qml/fxMenu/button/CustomButton.qml:10: TypeError: Property 'pressed' of object CustomButton_QMLTYPE_3(0x78214d8) is not a function
qml: F3 Key released!
qml: release function called
qml: Custom button released!

如果您想手动发出按下信号,请在您的 Button 对象中声明它:

Button {
    id: control
    signal pressed;
    signal released;

    function press() {
        down = true;
        pressed();
    }

    function release() {
        down = false;
        released()
    }

    onPressedChanged:  {
        if (down) {
            release();
        } else {
            press();
        }
    }
}

如您所述,还有一个名为 "pressed" 的布尔值 属性,因此 QML 引擎似乎无法识别您的意图。