main.qml Example File
mouseItem/qml/main.qml
import QtQuick 2.2
import QtQuick.Window 2.0
import QtGamepad 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Gamepad Mouse Item")
Text {
id: instructionLabel
anchors.centerIn: parent
text: qsTr("Simulate mouse input using a Gamepad")
}
Text {
id: outputLabel
anchors.horizontalCenter: instructionLabel.horizontalCenter
anchors.top: instructionLabel.bottom
text: ""
}
Connections {
target: GamepadManager
onGamepadConnected: gamepad1.deviceId = deviceId
}
Gamepad {
id: gamepad1
deviceId: GamepadManager.connectedGamepads.length > 0 ? GamepadManager.connectedGamepads[0] : -1
onButtonAChanged: {
if (value == true) {
gamepadMouse.mouseButtonPressed(Qt.LeftButton);
outputLabel.text = "Mouse click at: " + gamepadMouse.mousePosition.x + "," + gamepadMouse.mousePosition.y;
} else {
gamepadMouse.mouseButtonReleased(Qt.LeftButton);
outputLabel.text = "";
}
}
}
GamepadMouseItem {
id: gamepadMouse
anchors.fill: parent
gamepad: gamepad1
active: true
Rectangle {
id: cursor
width: 9
height: 9
radius: 4.5
x: gamepadMouse.mousePosition.x
y: gamepadMouse.mousePosition.y
color: "transparent"
border.color: "red"
Rectangle {
x: cursor.width * 0.5 - 0.5
y: 1
width: 1
height: cursor.height - 2
color: "black"
}
Rectangle {
x: 1
y: cursor.height * 0.5 - 0.5
height: 1
width: cursor.width - 2
color: "black"
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log("clicked at: " + mouse.x + "," + mouse.y);
}
}
}