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

Viewed 742

I am trying to implement a custom button which has press() and release() functions that i call when an expected key event is received. In these functions pressed() and released() signals are called. released() works perfectly but when pressed() is called an error is shown:

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

My theory is that the QML can't differentiate the Button's bool pressed property and the pressed() signal. Is this a bug or am i doing something wrong? Here's what i have done:

This is the custom button qml file:

import QtQuick 2.10
import QtQuick.Controls 2.12

Button {
    id: control

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

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

In the example below when the F3 key is pressed or released i call the button functions and i expect them to arrive to the Connections i made.

 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()
        }
    }

Like i said release works but the press is problematic. I see these lines in the console:

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!
1 Answers

If you want manually emit pressed signal, declare it in your Button object:

Button {
    id: control
    signal pressed;
    signal released;

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

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

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

As you mentioned, there is also a boolean property named "pressed", so it looks like QML engine does not recognize your intentions.

Related