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

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

我在QGraphicsScene和QGraphicsView上挣扎了两天。

当我点击视图的左上角(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()

鼠标按压事件QPoint(0,1)的协调性

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

绘制状态下的Q点坐标 x 0。

绘制状态下的Q点坐标为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个回答
2
投票

事实上,我很快就用.NET技术解决了我的问题。

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.