Mr.Tu
Mr.Tu

Reputation: 2733

how to imitate the button in QML

I want to imitate the Button in QML, but why does it only work with containsMouse and !containsMouse, it doesn't work with OnPressed. can you help me ? Thank you.

   Item {
        id: area
        property string str_noPressed;
        property string str_hover;
        property string str_pressed;

        Image {
            id: background
            width: 75; height: 75
            source: userArea.containsMouse ? str_hover : str_noPressed

            MouseArea {
                id: userArea
                anchors.fill: parent
                onClicked: {}
                onPressed: background.source = str_pressed
                hoverEnabled: true
            }
        }
    }

Upvotes: 1

Views: 702

Answers (1)

Chris Browet
Chris Browet

Reputation: 4266

Obviously, in the button is pressed, it also contains the mouse.

I assume containsMouse takes precedence over onPressed. Try:

onContainsMouseChanged:  {
    if (containsMouse && !pressed) {
        background.source = str_pressed
    }
}

Upvotes: 2

Related Questions