Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt Cursor Crosshair Lag



  • Hello,
    I am attempting to implement a crosshair onto a QtChart, that dynamically tracks the cursor's movement. However, I am experiencing a fair amount of lag between my cursor's position, and the painted crosshair lines. My current code is as follows;

    void SpectrumCoordinateDisplay::paint(QPainter* painter)
    {
        QRectF plotArea = m_chart->plotArea();
        QPointF crossHairPos = m_mousePos;
    
        if (!m_active || !plotArea.contains(crossHairPos))
        {
            return;
        }
    
        painter->setBrush(QColor(255, 255, 0, 255));
        painter->setPen(QColor(255, 255, 0, 255));
    
        const qreal rectSize = 6;
    
        QPointF left(plotArea.left(), crossHairPos.y());
        QPointF right(plotArea.right(), crossHairPos.y());
        QPointF top(crossHairPos.x(), plotArea.top());
        QPointF bottom(crossHairPos.x(), plotArea.bottom());
    
        QRectF crosshairRect(crossHairPos.x() - rectSize, crossHairPos.y() - rectSize, rectSize * 2, rectSize * 2);
        QPointF rectLeft(crosshairRect.left(), crossHairPos.y());
        QPointF rectRight(crosshairRect.right(), crossHairPos.y());
        QPointF rectTop(crossHairPos.x(), crosshairRect.top());
        QPointF rectBottom(crossHairPos.x(), crosshairRect.bottom());
    
        painter->drawLine(left, rectLeft);
        painter->drawLine(right, rectRight);
        painter->drawLine(top, rectTop);
        painter->drawLine(bottom, rectBottom);
        painter->drawLine(crosshairRect.topLeft(), crosshairRect.topRight());
        painter->drawLine(crosshairRect.topLeft(), crosshairRect.bottomLeft());
        painter->drawLine(crosshairRect.bottomRight(), crosshairRect.topRight());
        painter->drawLine(crosshairRect.bottomRight(), crosshairRect.bottomLeft());
    
        QRectF textRect = crosshairRect;
        textRect.moveTop(textRect.top() - textRect.height());
        textRect.setWidth(plotArea.width());
    
        textRect = painter->boundingRect(textRect, Qt::TextWordWrap, m_text);
        textRect.moveBottom(crosshairRect.top());
        textRect.moveLeft(crosshairRect.center().x() + 5);
    
        if (textRect.top() < plotArea.y())
            textRect.moveTop(crosshairRect.bottom());
        if (textRect.right() > plotArea.right())
            textRect.moveRight(crosshairRect.left());
    
        painter->drawText(textRect, Qt::TextWordWrap, m_text);
    }
    

    If anyone has any advice, please let me know.



  • What's the base class of SpectrumCoordinateDisplay?
    Did you have a look at the callout example which looks like it does what you are trying to do?



  • @VRonin
    Qt charts.
    The example is not really what I need, as I am basically trying to generate lines that follow the cursor perfectly with no trailing.


Log in to reply