学习 OpenCV2 – Qt 的 QGraphicsScene 坐标

关于 setSceneRect 前两个参数x y 不好理解,姑且这样理解 scene 就是场景, View 就是视角。当视图大于场景时,默认两者是中心对齐的;当视图小于场景时,两者左上对齐。 setSceneRect(x,y,w,h) 的参数 x,y 可以理解为 让视角相对于默认场景移动 x,y 。如果视角是不动的,所以看起来就像场景向相反方向移动了。

    //Scene & View
    QGraphicsScene *scene = new QGraphicsScene();
    scene->setSceneRect(100,50,40,30);

    scene->addPixmap(QPixmap::fromImage(img));

    // display scene area, item coordinates
    scene->addRect(0,0,40,30,QPen(Qt::red));


    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();

    //map
    qDebug() << "mapFromScene(0,0) is : " << ui->graphicsView->mapFromScene(0,0);
    qDebug() << "mapToScene(0,0) is : " << ui->graphicsView->mapToScene(0,0);


}

Debugging starts
mapFromScene(0,0) is :  QPoint(359,294)
mapToScene(0,0) is :  QPointF(-359,-294)
Debugging has finished

项目: QtUIOpenCV_scene_view

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.