Q3DSurface input handler with selection dragging



  • Hi!

    I'm implementing a custom input handler for Q3DSurface. I'd like to be able to select a point by pressing the left mouse button and drag the selection (change the selected point) according to the current mouse position as long as the button is pressed. When mouse leaves the area of the QSurface3DSeries it would be best to still select a point currently closest to the mouse cursor.

    I managed to make a version where the selection can be dragged (see bellow), however, when the cursor leaves the QSurface3DSeries area I'm only able to set the selected point to the last one. Is there a way to find the closest point easily?

    Also with the current solution when (with a pressed mouse button) I move the cursor away and than comes back into the QSurface3DSeries area the selection dragging is not re-instantiated and I'm left with the point which was the last selected one before leaving the QSurface3DSeries area. (This is because I'm not doing scene()->setSelectionQueryPosition(); the and QAbstract3DGraph::selectedElementChanged is not fired). Any ideas? Thanks!

    class SelectionDrag : public Q3DInputHandler
    {
    private:
        bool m_bLeftButtonPressed;
        Q3DSurface *m_pGraph;
        QPoint m_pLastPoint;
        QSurface3DSeries *m_pSeries;
    public:
        SelectionDrag(Q3DSurface *graph, QObject *parent) :
             Q3DInputHandler(parent),
             m_iSelectedItemType(QAbstract3DGraph::ElementNone),
             m_bLeftButtonPressed(false),
             m_bDragSeriesSelection(false),
             m__LastPosition(0,0),
             m__PrevPosition(0,0),
             m_pGraph(graph),
             m_pLastPoint(0,0),
             m_pSeries(nullptr)
        {
            connect(graph, &QAbstract3DGraph::selectedElementChanged, this,
                    &SelectionDrag::handleElementSelected);
        }
    
        void handleElementSelected(QAbstract3DGraph::ElementType type)
        {
            m_iSelectedItemType = type;
        }
        void mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos)
        {
            if(m_bLeftButtonPressed)
            {
                if(QAbstract3DGraph::ElementSeries == m_iSelectedItemType)
                {
                    scene()->setSelectionQueryPosition(mousePos);
                    m_pSeries = m_pGraph->selectedSeries();
                    m_pLastPoint = m_pSeries->selectedPoint();
                }
                else
                {
                    m_pSeries->setSelectedPoint(m_pLastPoint);
                }
            }
            else
            {
                Q3DInputHandler::mouseMoveEvent(event, mousePos);
                qDebug() << "No Sel";
            }
        }
        void mousePressEvent(QMouseEvent *event, const QPoint &mousePos)
        {
            Q3DInputHandler::mousePressEvent(event, mousePos);
            if (Qt::LeftButton == event->button())
            {
                m_bLeftButtonPressed = true;
            }
        }
    
        void mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos)
        {
            Q3DInputHandler::mouseReleaseEvent(event, mousePos);
            m_bLeftButtonPressed = false;
        }
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.