bvs
bvs

Reputation: 350

QRect not centering in widget although QCursor is with same val?

I can set the QCursor to the center of the GLWidget, even when that GLWidget is within a layout or splitters. However, I want to draw something in 2D with qPainter instead of setting the cursor shape - at the center of the screen.

I cannot align QRect to the center of the screen - the same value puts the ellipse at the bottom right of the GLWidget. Why don'these center coordinates work in both cases?

void GLWidget::paintEvent(QPaintEvent *)
{
    if (selCam->camType->val_s == "fps")
    {
        QPoint p = mapFromGlobal(QCursor::pos());

        fpsCenter = mapToGlobal(QPoint(width() / 2, height() / 2));
        //fpsCenter = 738, 549 
        QCursor::setPos(fpsCenter);
    }

    //other rendering 

    if (selCam->camType->val_s == "fps")
    {
        QRect rectAim(10, 10, 20, 20);

        rectAim.moveCenter(fpsCenter);

        painter.setPen(Qt::black);
        painter.drawEllipse((rectAim));
    }
}

enter image description here

Upvotes: 1

Views: 291

Answers (1)

vahancho
vahancho

Reputation: 21220

I think the problem is that you use global coordinates for the painter, however it should use the relative coordinates. So you have to move your rectangle in the center of the viewport or your widget, I.e.:

rectAim.moveCenter(rect().center());

Upvotes: 1

Related Questions