QGraphicsView
//当前项矩阵
void transform();
//转换至视口坐标矩阵
void viewportTransform();
//根据视口坐标提取项
void itemAt();
void items();
//视口坐标 -> 场景坐标
void mapToScene();
//场景坐标 -> 视口坐标
void mapFromScene();
QGraphicsScene
//根据场景坐标提取项
void itemAt();
void items();
QGraphicsItem
//当前项矩阵
void transform();
//转换至场景矩阵
void sceneTransform();
//转换至其它项矩阵
void itemTransform();
//外部视口坐标 -> 内部视口坐标
void mapFromItem();
//父类视口坐标 -> 内部视口坐标
void mapFromParent();
//场景坐标 -> 视口坐标
void mapFromScene();
//内部视口坐标 -> 外部视口坐标
void mapToItem();
//内部视口坐标 -> 父类视口坐标
void mapToParent();
//视口坐标 -> 场景坐标
void mapToScene();
测试
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView view;
QGraphicsScene* scene=new QGraphicsScene();
view.setScene(scene);
scene->addLine(-200,0,200,0);
scene->addLine(0,-200,0,200);
QGraphicsRectItem* rect1=new QGraphicsRectItem();
rect1->setPos(-25,-25);
rect1->setRect(QRect(0,0,100,100));
scene->addItem(rect1);
QGraphicsRectItem* rect2=new QGraphicsRectItem();
rect2->setPos(25,25);
rect2->setRect(QRect(0,0,100,100));
scene->addItem(rect2);
qDebug()<<rect1->mapFromItem(rect2,QPointF(0,0));
qDebug()<<rect1->parentItem()<<rect1->mapFromParent(QPointF(0,0));
qDebug()<<rect1->mapFromScene(QPointF(0,0));
qDebug()<<rect1->mapToItem(rect2,QPointF());
qDebug()<<rect1->parentItem()<<rect1->mapToParent(QPointF(0,0));
qDebug()<<rect1->mapToScene(QPointF(0,0));
view.show();
return a.exec();
}
运行结果
QPointF(50,50)
QGraphicsItem(0) QPointF(25,25)
QPointF(25,25)
QPointF(-50,-50)
QGraphicsItem(0) QPointF(-25,-25)
QPointF(-25,-25)
QGraphicsView坐标系统