qmlscatter3d

Scatter3D graphs in QML


I want to add rotation of graphs feature to the below scatter 3D graphs :

Scatter3D {
    id: scatter3d
    width: parent.width
    height: parent.height
    anchors.centerIn: parent
    theme: Theme3D {
        backgroundColor: "black"
    }

    Scatter3DSeries {
        baseColor: "red"
        ItemModelScatterDataProxy {
            itemModel: dataModel
            //Mapping model roles to scatter series item coordinates.
            xPosRole: "xPos"
            yPosRole: "yPos"
            zPosRole: "zPos"
        }
    }
}

How to add rotation feature ?


Solution

  • Information from Qt Data Visualization Interacting with Data.

    Graphs can be rotated freely by holding down the right mouse button and moving the mouse.

    More information from the same page.

    Qt Data Visualization has default handlers for mouse actions and touch gestures. For the default mouse controls, see Q3DInputHandler, and for the default touch controls, see QTouch3DInputHandler. The default handlers must be disabled when using customized input handlers.

    When you look into the implementation of q3dinputhandler.cpp you can see in the Q3DInputHandler::mousePressEvent() function that the rotation is hard coded to the Qt::RightButton button. In the default implementation you can only enable/disable the rotation via the rotationEnabled property of an InputHandler instance.

    The only way of changing the InputHandler3D behavior, e.g. changing the button with which the rotation is triggered, is by writing a custom InputHandler3D and bind it to the inputHandler property of your Scatter3D instance. This should be simple. You need to create a C++ class (CustomInputHandler) that inherits from Q3DInputHandler or QAbstract3DInputHandler, overwrite the mousePressEvent() function and make it available in your QML context.