Custom Input Example▲
The Custom Input example shows how to customize the 3D graph controls in a widget application using a custom graph input handler to capture and process mouse events. The code in this example shows also how the camera is controlled by using QPropertyAnimation 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.
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.
Replacing Default Input Handling▲
The default input handling mechanism is replaced by setting the active input handler of Q3DScatter to CustomInputHandler that implements the custom behavior.
m_graph-&
gt;setActiveInputHandler(m_inputHandler);
Implementing Custom Selection Handling▲
The on mouseover selection handling is implemented in the CustomInputHandler that captures the mouse events. It then stores the last known coordinates to the QAbstract3DInputHandler::inputPosition property.
void
CustomInputHandler::
mouseMoveEvent(QMouseEvent *
event, const
QPoint &
amp;mousePos)
{
Q_UNUSED(event)
setInputPosition(mousePos);
}
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.
m_selectionTimer =
new
QTimer(this
);
m_selectionTimer-&
gt;setInterval(10
);
m_selectionTimer-&
gt;setSingleShot(false
);
QObject::
connect(m_selectionTimer, &
amp;QTimer::
timeout, this
,
&
amp;ScatterDataModifier::
triggerSelection);
m_selectionTimer-&
gt;start();
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.
void
CustomInputHandler::
wheelEvent(QWheelEvent *
event)
{
// Adjust zoom level based on what zoom range we're in.
int
zoomLevel =
scene()-&
gt;activeCamera()-&
gt;zoomLevel();
if
(zoomLevel &
gt; 100
)
zoomLevel +=
event-&
gt;angleDelta().y() /
12
;
else
if
(zoomLevel &
gt; 50
)
zoomLevel +=
event-&
gt;angleDelta().y() /
60
;
else
zoomLevel +=
event-&
gt;angleDelta().y() /
120
;
if
(zoomLevel &
gt; 500
)
zoomLevel =
500
;
else
if
(zoomLevel &
lt; 10
)
zoomLevel =
10
;
scene()-&
gt;activeCamera()-&
gt;setZoomLevel(zoomLevel);
}