qmlqt5qtquick2mouse-cursorqtquickcontrols

How to change the cursor's shape without using MouseArea?


I looked over the similar threads but it didn't help much.

I'm using QtQuick.Controls.Button in QML and I cannot change the cursor shape when hovering over the button! I want to achieve this without using MouseArea. What can be done? As I looked over the documentation I couldn't find a, say, cursorShape property or similar.


Solution

  • It is kind of a hack, but you can access the Button's own MouseArea via the __behavior pseudo-private property.

    Button {
        text: qsTr("Hello World")
        Component.onCompleted: __behavior.cursorShape = Qt.PointingHandCursor
    }
    

    Alternatively, you can very easily create your own improved Button:

    import QtQuick 2.3
    import QtQuick.Controls 1.2
    
    Button {
        property alias cursorShape: mouseArea.cursorShape
    
        MouseArea
        {
            id: mouseArea
            anchors.fill: parent
            onPressed:  mouse.accepted = false
        }
    }
    

    Note that you may have to explicitly import the QML module where you defined Button in order to overshadow the QtQuick.Controls's Button.