Qt Quick 2 Custom Input Example

The Qt Quick 2 Custom Input example shows how to customize the 3D graph controls from Qt Quick 2 using the MouseArea to capture and process mouse events in QML. Custom input handling code in this example shows how the camera is now controlled by using NumberAnimation to animate the camera and item selection is done on mouseover rather than clicking any mouse buttons. Also the code shows how to implement similar zoom with mouse wheel functionality as the default input handler implements.

Image non disponible

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Removing Default Input Handling

The default input handling mechanism is disabled by setting the inputHandler property to null.

 
Sélectionnez
Scatter3D {
...
inputHandler: null
...

Implementing Custom Selection Handling

The on mouseover selection handling is implemented using standard MouseArea to capture the mouse events. The mouse area is configured to capture hover events and has two custom properties for mouseX and mouseY to store the last known mouse coordinates.

 
Sélectionnez
MouseArea {
    id: inputArea
    anchors.fill: parent
    hoverEnabled: true
    acceptedButtons: Qt.LeftButton | Qt.RightButton
    property int mouseX: -1
    property int mouseY: -1

Whenever a pointer movement related signal is received the code updates the mouseX and mouseY properties.

 
Sélectionnez
onPositionChanged: {
    mouseX = mouse.x;
    mouseY = mouse.y;
}

As the selection is one shot, and is cleared each time a 3D frame is rendered, a timer is setup to retrigger selection so that the selection moves to the item currently under the mouse cursor as the camera animates around the graph even when the mouse cursor is not moving.

 
Sélectionnez
Timer {
    id: reselectTimer
    interval: 10
    running: true
    repeat: true
    onTriggered: {
        scatterGraph.scene.selectionQueryPosition = Qt.point(inputArea.mouseX, inputArea.mouseY);
    }
}

Implementing Custom Zoom Handling

The camera has a zoom factor that represents amount of zoom in percentages. In this example the zoom range is limited between 10% and 500%. This range is then divided to four subranges where angleDelta is scaled to different amount of zoom change based on the current subrange.

 
Sélectionnez