0

I'm struggling for 2 days with my QGraphicsScene and QGraphicsView.

When I click on the top left corner of my view (0,0), the mouse click event get the 0,0 but when I add item to the scene, it gives different coordinates and I can't find why. I think the problem is inside the scene->addItem

Here is the code :

void GraphBoard::drawState(QPoint cpoint)
{

    qDebug() << "Coordonnées QPoint dans drawState "<< cpoint;
    qDebug() << "Coordonnées QPoint dans drawState x"<< cpoint.x();
    qDebug() << "Coordonnées QPoint dans drawState y"<< cpoint.y();
    QGraphicsEllipseItem * mellipse = new QGraphicsEllipseItem(cpoint.x(),cpoint.y(),100,100);
    QPen pen;
    pen.setWidth(8);
    mellipse->setPen(pen);
    scene->addItem(mellipse);


}



void GraphBoard::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "Coordonnées Mouse Press Event "<<event->pos();
    if(globalAddStateMode==true)

    {
        if (event->button() == Qt::LeftButton) {
            QPoint clickLocation=event->pos();
           drawState(clickLocation);
           globalAddStateMode=false;
        }
    }

}

The qDebug()

Coordonnées Mouse Press Event QPoint(0,1)

Coordonnées QPoint dans drawState QPoint(0,1)

Coordonnées QPoint dans drawState x 0

Coordonnées QPoint dans drawState y 1

The result on my program the circle is supposed to be on the top left corner

The only things I did with the scene are:

 scene = new QGraphicsScene();

        QPen pen;

        //axis
        pen.setStyle(Qt::DashLine);
        scene->addLine(0,-800,0,800,pen);
        scene->addLine(-800,0,800,0,pen); //horizontal line

view->setScene(scene);
    scene->setBackgroundBrush(Qt::gray);

1 Answer 1

2

Actually I quickly solved my problem with :

QPointF clickLocation=mapToScene(event->pos());

And with an offset -50, -50:

QGraphicsEllipseItem * mellipse = new QGraphicsEllipseItem(cpoint.x()-50,cpoint.y()-50,100,100);

to make the circle appear arround the mouse

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.