Qt:视图坐标和视图坐标添加项目之间的问题

问题描述 投票:0回答:1

我在使用QGraphicsScene和QGraphicsView的过程中挣扎了2天。

当我单击视图的左上角(0,0)时,鼠标单击事件将获得0,0,但是当我向场景中添加项目时,它给出了不同的坐标,我找不到原因。我认为问题出在场景内-> addItem

这里是代码:

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;
        }
    }

}

qDebug()

[Coordonnées鼠标新闻事件QPoint(0,1)

[CoordonnéesQPoint和drawState QPoint(0,1)

[CoordonnéesQPoint dans drawState x 0

[CoordonnéesQPoint dans drawState y 1

我程序的结果the circle is supposed to be on the top left corner

我对场景所做的唯一事情是:

 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);
qt qgraphicsview
1个回答
1
投票

实际上,我很快就通过:]解决了我的问题>

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

且偏移量为-50,-50:

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

使圆圈出现在鼠标周围

© www.soinside.com 2019 - 2024. All rights reserved.